使用FLEX和Actionscript开发FLASH 游戏-碰撞检测(第四页)
main.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:Application
xmlns:ms="http://www.adobe.com/2006/mxml"
layout="absolute"
width="600"
height="400"
frameRate="100"
creationComplete="creationComplete()"
enterFrame="enterFrame(event)"
click="click(event)"
mouseDown="mouseDown(event)"
mouseUp="mouseUp(event)"
mouseMove="mouseMove(event)"
currentState="MainMenu">
<mx:states>
<mx:State
name="Game"
enterState="enterGame(event)"
exitState="exitGame(event)"
</mx:State>
<mx:State name="MainMenu">
<mx:AddChild relativeTo="{myCanvas}" position="lastChild">
<mx:Button x="525" y="368" label="Start" id="btnStart" click="startGameClicked(event)"/>
</mx:AddChild>
</mx:State>
</mx:states>
<mx:Canvas x="0" y="0" width="100%" height="100%" id="myCanvas"/>
<mx:Script>
<![CDATA[
protected var inGame:Boolean=false;
public function creationComplete():void
{
GameObjectManager.Instance.addCollidingPair(CollisionIdentifiers.PLAYER,CollisionIdentifiers.ENEMY);
GameObjectManager.Instance.addCollidingPair(CollisionIdentifiers.ENEMY,CollisionIdentifiers.PLAYERWEAPON);
GameObjectManager.Instance.addCollidingPair(CollisionIdentifiers.PLAYER,CollisionIdentifiers.ENEMYWEAPON);
}
public function enterFrame(event:Event):void
{
if(inGame)
{
GameObjectManager.Instance.enterFrame();
myCanvas.graphics.clear();
myCanvas.graphics.begingBitmapFill(GameObjectManager.Instance.backBuffer,null,false,false);
myCanvas.graphics.drawRect(0,0,this.width,this.height);
myCanvas.graphics.endFill();
}
}
privated function click(event:MouseEvent):void
{
GameObjectManager.Instance.click(event);
}
privated function mouseDown(event:MouseEvent):void
{
GameObjectManager.Instance.mouseDown(event);
}
privated function mouseUp(event:MouseEvent):void
{
GameObjectManager.Instance.mouseUp(event);
}
privated function mouseMove(event:MouseEvent):void
{
GameObjectManager.Instance.mouseMove(event);
}
protected function startGameClicked(event:Event):void
{
currentState="Game"
}
protected function enterGame(event:Event):void
{
Mouse.hide();
GameObjectManager.Instance.startup();
Level.Instance.startup();
inGame=true;
}
protected function exitGame(event:Event):void
{
Mouse.show();
Level.Instance.shutdown();
GameObjectManager.Instance.shutdown();
inGame=false;
}
]]>
</mx:Script>
</mx:Application>
就如你所见的要指定两个游戏对象碰撞只需要调用GameObjectManager的addCollidingPair函数。这里我们指定了游戏者会同敌机碰撞,敌机会同游戏者子弹碰撞,游戏者也会与敌机子弹碰撞。
那么现在既然我们可以检测到碰撞我们就需要更新游戏者、子弹和敌机类的代码,来设置他们的碰撞名以及当检测到时的反应。现在让我们看看游戏者类。
<Previous Page | page: 1 2 3 4 5 6 | Next Page>