使用Flash CC的HTML5 Canvas游戏中的HitTest
HitTest in HTML5 Canvas game using Flash CC
我正在Flash CC中制作HTML5 Canvas游戏。然而,我似乎不知道如何在两部电影之间创建一个hitTest。我在EaselJS网站上找到了一些代码,但它不起作用,或者我不明白它是如何工作的。
这是我在操作面板中的代码:
this.addEventListener("tick",move.bind(this));
function move(){
if (collision(this.bird, this.bar)){
this.bird.play(5);
}
}
function collision(a,b) {
var locA = a.globalToLocal(100,0);
if (b.hitTest(locA.x, locA.y)) {
return true;
}
}
好的,所以我想Flash或EaselJS中还没有内置的功能,所以我想出了这个,它工作正常。但它只检查hitBox,矩形MovieClip边界,而不是形状中实际可见的像素。令人沮丧的是,在Flash for HTML5画布中,甚至连宽度和高度都不支持movieclip实例的属性,所以你只需要在属性面板中查找这些属性。不管怎样,以下是我的想法:
var objA = this.bullet ; // The selected MovieClip
var objA_width = 20; // enter selected Movieclip's width (you can't find out with script, duh!)
var objA_height = 10; // enter selected Movieclip's height
var objB = this.target; // The MovieClip to test collision with
var objB_width = 100; // enter it's width
var objB_height = 100; // ...and it's height
this.addEventListener("tick",hitTest.bind(this));
function hitTest(){
if (collision(objA, objB, objA_width,objA_height,objB_width,objB_height)){
// code to run on collision;
}
}
var mca = new Object();
var mcb = new Object();
function collision(a,b,aWidth,aHeight,bWidth,bHeight) {
mca.xr = a.x + (aWidth/2); //the right edge of movieclip A
mca.xl = a.x - (aWidth/2); //the left edge of movieclip A
mca.yt = a.y - (aHeight/2); //the top edge of movieclip A
mca.yb = a.y + (aHeight/2); //the bottom edge of movieclip A
mcb.xr = b.x + (bWidth/2);
mcb.xl = b.x - (bWidth/2);
mcb.yt = b.y - (bHeight/2);
mcb.yb = b.y + (bHeight/2);
xHit = mca.xr > mcb.xl && mca.xl < mcb.xr; // returns true if the is any horisontal overlappnig
yHit = mca.yt < mcb.yb && mca.yb > mcb.yt; // returns true if the is any vertical overlapping
if (xHit && yHit){return true;}
}
相关文章:
- 如何使用phaser使html5游戏在移动设备浏览器上运行
- 如何有效地将游戏数据存储在URL查询字符串中
- Javascript游戏输入失去焦点
- 如何在p5.js中管理多智力竞赛游戏的多个屏幕
- setInterval游戏循环的范围问题
- 为网络游戏制作动画
- 重置游戏和获胜者问题
- 帆布游戏的滞后运动
- Javascript单独的游戏窗口
- Javascript猜测游戏-计数器不工作
- JavaScript纸牌游戏
- 简单游戏的画布与SVG
- 我的HTML5游戏不时暂停,我如何才能知道它是否's是由垃圾收集引起的
- 多人游戏完全在浏览器中运行,服务器仅用于数据库
- 通过PHP将单词添加到游戏中
- 2D Processingjs游戏中的冲突
- Javascript岩石,纸,剪刀游戏
- 设置变量时破坏游戏代码
- JavaScript中的生活游戏-使用表
- 使用Flash CC的HTML5 Canvas游戏中的HitTest