创建一个bullet类:
class bullet extends egret.Sprite{
public constructor(color,x,y,r){ //构造函数。
super(); //继承
var bullet_obj=new egret.Shape();
bullet_obj.graphics.beginFill(color);
bullet_obj.graphics.drawCircle(x,y,r);
bullet_obj.graphics.endFill();
this.addChild(bullet_obj);
this.bullet_move(); //每创创建一个bullet就移动
}
public bullet_move(){
//每刷新一帧子弹向上移动
this.addEventListener(egret.Event.ENTER_FRAME,()=>{this.y-=2;
},this);
}
}
在创建游戏场景场景中写如下代码:
bullet1:bullet;
private createGameScene() {
//添加点击事件,点击鼠标后在该位置产生一个子弹。evt.localX,evt.localY为鼠标的坐标
this.stage.$addListener(egret.TouchEvent.TOUCH_TAP,(evt:egret.TouchEvent)=>{
this.bullet1=new bullet('0xFF0000',evt.localX,evt.localY,'20');
this.addChild(this.bullet1);
},this);
}