您好我试图创建一个类似于太空入侵者的游戏,并且发现很难创建一个hitTestObject,所以如果外星人击中mc_gun,玩家就会失去生命,一旦他失去了三个生命,游戏就结束了被带到一个屏幕,他/她可以输入他们的名字进入高分榜。
我仍然需要做以下事情,但不知道从哪里开始...
为玩家创造三个生命。 所有生命都消失后结束游戏。 采取进入名称屏幕 高分榜。
任何帮助将不胜感激。
这是我在文件正文中的编码......
stop();
var score:Number = 0;
var livesLeft:Number = 3;
var lifesArray:Array = new Array();
lifesArray.push(life1);
lifesArray.push(life2);
lifesArray.push(life3);
addEventListener(Event.ENTER_FRAME, pulse);
function pulse(event:Event):void
{
checkForHitOnGun();
checkForHitOnAliens();
checkForBulletOnBulletHit();
//checkEndGameCondition
}
function checkForHitOnGun()
{
for (var i=0; i<numChildren; i++)
{
if (getChildAt(i) is alienbullet)
{
var mc_alienbullet:MovieClip = getChildAt(i) as MovieClip;
for (var j=0; j<numChildren; j++)
{
if (getChildAt(j) is gun)
{
var mc_gun:MovieClip = getChildAt(j) as MovieClip;
if (mc_alienbullet.hitTestObject(mc_gun))
{
health -= 10;
if (health<1){
if(livesLeft<1){
gotoAndPlay(2);
removeChild(mc_gun);
stage.removeEventListener(Event.ENTER_FRAME,pulse);
}
livesLeft--;
lifesArray[livesLeft].visible = false;
health=100;
if (livesLeft == 0){
(endGo);
gotoAndPlay(2);
}
}
trace("health =" +health);
}
}
}
}
}
}
function checkForHitOnAliens()
{
for (var i=0; i<numChildren; i++)
{
if (getChildAt(i) is bullet)
{
var mc_bullet:MovieClip = getChildAt(i) as MovieClip;
for (var j=0; j<numChildren; j++)
{
if (getChildAt(j) is alien)
{
var mc_alien:MovieClip = getChildAt(j) as MovieClip;
if (mc_bullet.hitTestObject(mc_alien))
{
var tobeDestroyed:MovieClip;
if (mc_alien.x > mc_bullet.x)
{
tobeDestroyed = mc_alien;
}
else
{
tobeDestroyed = mc_alien;
}
var bomb_mc:explosion = new explosion();
bomb_mc.x = tobeDestroyed.x;
bomb_mc.y = tobeDestroyed.y;
var addspaceship:alien = new alien();
addspaceship.x = 600;
addspaceship.y = Math.random()*(stage.stageHeight);
tobeDestroyed.destroy();
addChild(bomb_mc);
addChild(addspaceship);
score++;
return;
}
}
tb_score.text = "" + score;
}
}
}
}
function checkForBulletOnBulletHit()
{
for (var i=0; i<numChildren; i++)
{
if (getChildAt(i) is bullet)
{
var mc_bullet:MovieClip = getChildAt(i) as MovieClip;
for (var j=0; j<numChildren; j++)
{
if (getChildAt(j) is alienbullet)
{
var mc_alienbullet:MovieClip = getChildAt(j) as MovieClip;
if (mc_bullet.hitTestObject(mc_alienbullet))
{
var tobeDestroyed:MovieClip;
if (mc_bullet.x > mc_alienbullet.x)
{
tobeDestroyed = mc_bullet;
}
else
{
tobeDestroyed = mc_alienbullet;
}
/*score++;
tb_score.text = "" + score;*/
var bomb_mc:explosion = new explosion();
bomb_mc.x = tobeDestroyed.x;
bomb_mc.y = tobeDestroyed.y;
tobeDestroyed.destroy();
addChild(bomb_mc);
/*if (mc_bullet.hitTestObject(mc_alienbullet))
{
score++;
mc_bullet.gotoAndPlay(2);
}*/
score++;
return;
}
tb_score.text = "" + score;
}
}
}
}
}
//mc_gun would make the bullet fire only if the gun is clicked.
//the game is to difficult so i changed it to stage. to make the whole stage the fireing area.
stage.addEventListener(MouseEvent.CLICK, fire);
function fire(event:MouseEvent):void
{
var bulletfire:bullet = new bullet();
bulletfire.x = mc_gun.x - 20 + mc_gun.width;
bulletfire.y = mc_gun.y;
addChild(bulletfire);
}
mc_banner.addEventListener(MouseEvent.CLICK, startGo);
function startGo(event:MouseEvent):void
{
mc_banner.visible = false;
mc_instructions.visible = false;
mc_highscore.visible = false;
mc_gun.visible = true;
tb_score.visible = true;
addEventListener(Event.ENTER_FRAME, pulse);
var initialspaceship:alien=new alien();
initialspaceship.x = 600;
initialspaceship.y=Math.random()*(stage.stageHeight);
addChild(initialspaceship);
}
function endGo()
{
mc_gun.visible = false;
tb_score.visible = false;
removeEventListener(Event.ENTER_FRAME, pulse);
mc_banner.visible = true;
livesLeft--;
lifesArray[livesLeft].visible = false;
}
答案 0 :(得分:0)
你的engGo()
函数是负责减少生命的人
这是检查玩家是否已经死亡(0人)并结束游戏的最佳场所
我可以理解阅读您的代码,livesLeft
是您存储播放器生命的地方
在lifesArray[livesLeft].visible = false;
之后添加以下内容:
if (livesLeft <= 0)
{
defeat();
}
现在您必须从GamePlay更改为HighScore 如果您在Flash Pro的框架中工作,则只需转到具有HighScore
的框架function defeat():void
{
gotoAndStop("HighScore");
}
否则(你需要在一个帧中完成所有操作),你需要清除舞台并在其上添加新内容。
function defeat():void
{
// First, remove ALL listeners
removeEventListener(Event.ENTER_FRAME, pulse);
stage.removeEventListener(MouseEvent.CLICK, fire);
// Then, we remove everything
removeChild(...);
// Finally, you add the new stuff
addChild(...);
}
如果你要采用第二种方法,我建议你将你的功能解耦并将它们包装在课堂中,这样你的游戏逻辑就可以在一个地方(在一个班级中),你的高分屏幕将是在另一个,你可以轻松交换它们。