flash中如何检测飞出的子弹和敌人相撞,求源码,求详细解释。 50
正在编写一个小游戏,用到这部分知识,可是我不会publicfunctioncheckCollisionWithEnemies(boll){if(enemy!=null){...
正在编写一个小游戏,用到这部分知识,可是我不会
public function checkCollisionWithEnemies(boll)
{
if (enemy != null)
{
if(enemy.hitTestPoint(boll.x, boll.y, true))
{
trace ("hello!");
}
}
}
运行后没反应,也没报错。怎么回事? 展开
public function checkCollisionWithEnemies(boll)
{
if (enemy != null)
{
if(enemy.hitTestPoint(boll.x, boll.y, true))
{
trace ("hello!");
}
}
}
运行后没反应,也没报错。怎么回事? 展开
展开全部
1.计算距离
var Apoint:Point = new Point(bullet.x, bullet.y);
var Bpoint:Point = new Point(enemy.x, enemy.y);
var distanceAB:Number = Point.distance(Apoint, Bpoint);
if (distanceAB < ???)
{
//碰撞
}
2.像素级别的碰撞,消耗挺大的,如果没必要很精确的话,不推荐!
package
{
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.geom.Matrix;
import flash.geom.Point;
public class BitmapCollision extends Sprite
{
private var bmpd1:BitmapData = new BitmapData(stageWidth, stageHeight,true,0);
private var bmpd2:BitmapData = new BitmapData(stageWidth, stageHeight,true,0);
private var spr1:Sprite = new Sprite;
private var spr2:Sprite = new Sprite;
public function BitmapCollision(spr1:Sprite,spr2:Sprite)
{
this.spr1 = spr1;
this.spr2 = spr2;
}
public function updataHandler():void
{
bmpd1.fillRect(bmpd1.rect, 0);
bmpd2.fillRect(bmpd2.rect, 0);
bmpd1.draw(spr1, new Matrix(1, 0, 0, 1, spr1.x, spr1.y));
bmpd2.draw(spr2, new Matrix(1, 0, 0, 1, spr2.x, spr2.y));
if (bmpd1.hitTest(new Point(), 255, bmpd2, new Point(), 255))
{
//碰撞
}
else
{
}
}
}
}
3. as 自带的 hitTest!
方法很多,找到适合自己的!
var Apoint:Point = new Point(bullet.x, bullet.y);
var Bpoint:Point = new Point(enemy.x, enemy.y);
var distanceAB:Number = Point.distance(Apoint, Bpoint);
if (distanceAB < ???)
{
//碰撞
}
2.像素级别的碰撞,消耗挺大的,如果没必要很精确的话,不推荐!
package
{
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.geom.Matrix;
import flash.geom.Point;
public class BitmapCollision extends Sprite
{
private var bmpd1:BitmapData = new BitmapData(stageWidth, stageHeight,true,0);
private var bmpd2:BitmapData = new BitmapData(stageWidth, stageHeight,true,0);
private var spr1:Sprite = new Sprite;
private var spr2:Sprite = new Sprite;
public function BitmapCollision(spr1:Sprite,spr2:Sprite)
{
this.spr1 = spr1;
this.spr2 = spr2;
}
public function updataHandler():void
{
bmpd1.fillRect(bmpd1.rect, 0);
bmpd2.fillRect(bmpd2.rect, 0);
bmpd1.draw(spr1, new Matrix(1, 0, 0, 1, spr1.x, spr1.y));
bmpd2.draw(spr2, new Matrix(1, 0, 0, 1, spr2.x, spr2.y));
if (bmpd1.hitTest(new Point(), 255, bmpd2, new Point(), 255))
{
//碰撞
}
else
{
}
}
}
}
3. as 自带的 hitTest!
方法很多,找到适合自己的!
AiPPT
2024-09-19 广告
2024-09-19 广告
随着AI技术的飞速发展,如今市面上涌现了许多实用易操作的AI生成工具1、简介:AiPPT: 这款AI工具智能理解用户输入的主题,提供“AI智能生成”和“导入本地大纲”的选项,生成的PPT内容丰富多样,可自由编辑和添加元素,图表类型包括柱状图...
点击进入详情页
本回答由AiPPT提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询