我是ActionScript的新手,我有一个问题。
我在课堂上,“敌人”。该类具有“碰撞”功能。我怎样才能与玩家在这堂课中留下的生命数量进行沟通?谢谢。
答案 0 :(得分:2)
尝试这样的事情:
public class Game extends MovieClip {
public var enemy:Enemy; //enemy can be a timeline instance
public var player:Player //can also be a timeline instance
public function Game() {
super();
//for this to work enemy must exist on frame one of the Game MC
//and stay around for the rest of the MC's lifespan
enemy.addEventListener(EnemyEvent.COLLISION, onEnemyCollision);
}
protected function onEnemyCollision(e:EnemyEvent):void {
e.player.lives--;
}
}
//the enemy!!!
public class Enemy extends Sprite {
//I actually don't understand why this is a function on enemy.
//I wouldn't have designed it this way.
//What is calling it?
public function collision(withPlayer:Player):void {
dispatchEvent(new EnemyEvent(EnemyEvent.COLLISION, withPlayer));
}
}
//the player
public class Player extends Sprite {
public var lives:int=10;
}
//the enemy event
public class EnemyEvent extends Event {
public static const COLLISION:String = 'Big badda boom.';//Fifth Element reference
public var player:Player;
public function EnemyEvent(type:String, player:Player) {
super(type, true, true);
this.player = player;
}
public function clone():Event {
new EnemyEvent(type, player);
}
}
如果你不想在第一帧上拥有玩家和敌人,请查看这篇文章,了解更深入地了解这些内容的工作原理:http://www.developria.com/2010/04/combining-the-timeline-with-oo.html。或者你可以编写代码来手动添加它们,但这太像工作了。