我有以下代码,它适用于类似于太空入侵者的基本自上而下射击游戏。当玩家的子弹击中敌人时,敌人会正确地跟随各种命中功能并在三分之一秒内闪烁不可见。然而,当玩家被击中时,尽管使用了相同的功能,他似乎永久地变成了隐形。关于为什么会发生这种情况以及我哪里出错的任何想法?我的代码可能会被那些有经验的人认为是混乱的,所以请尽可能多地批评:P代码背后的结构和过程在我的脑海中是有意义的,但如果有人在努力,请随意问“wtf是你吗?做“:P
package {
import flash.display.MovieClip;
import flash.display.Stage;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.utils.Timer;
import flash.geom.ColorTransform;
public class Level1 extends MovieClip {
public var player:Player; // play
public var upgrade:blue_wep;
public var enemy1:fly; // first enemy used for testing
public var enemyList:Array = []; // Array similar to bullets but for each enemy
public var LevelTimer:Timer; // creating a game timer used to see how long the player survived
public var upgraded:Boolean = false;
//Code for shooting bullets
public var bulletList:Array = []; //This array simply contains information for each bullet
public var enemyBullets:Array = [];
public var mousePressed:Boolean = false; //boolean similar to player movement but this checks if the mouse button is pressed
public var weaponSpeed:Number = 1; // This is an amplifier to modify the player's shooting speed when collecting powerups
public var delayCounter:int = 0; //used to create delay between shots
public var delayMax:Number = 10 * weaponSpeed; //This is the number that defines how quickly we can shoot
//The following variables are for when the fly gets hit by a bullet
public var losingHealth:Boolean = false;
public var hitTimer:int;
public var deathTimer:int;
public var hurt:Boolean = false;
public var deathFlash:ColorTransform = new ColorTransform;
public function Level1() {
// constructor code
player = new Player(stage, stage.stageWidth/2, stage.stageHeight/2, 100, 100, 10);
stage.addChild(player);
upgrade = new blue_wep(stage, stage.stageWidth/6, stage.stageHeight/2, 5);
enemy1 = new fly(stage, (Math.random() * stage.stageWidth), (Math.random() * stage.stageHeight), player, 100, 100, 20);
stage.addChild(enemy1);
LevelTimer = new Timer(1000);
hitTimer = 0;
deathTimer = 0;
deathFlash.color = 0xFFFFFF;
// is clicked. This works for single fire
stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler, false, 0, true);
stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler, false, 0, true);
stage.addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
}
public function loop(e:Event):void { // this function is the loop for adding bullets that move
if(mousePressed) {
delayCounter++
if (delayCounter == delayMax) {
PlayershootBullet();
delayCounter = 0;
}
}
if (bulletList.length > 0) { //if there are any bullets on the screen we are going to execute the following code
for (var i:int = 0; i < bulletList.length; i++) { //the following code will be executed on each bullet
bulletList[i].loop(); // we are going to use the loop function found in the bullet class file on each bullet
}
}
enemyBullets = enemy1.bulletList;
collisionCheck(bulletList, player, enemy1);
if (losingHealth == true) {
healthLoss(player, enemy1);
}
else {
losingHealth = false;
collisionCheck(enemyBullets, enemy1, player);
if (losingHealth == true) {
healthLoss(enemy1, player);
}
}
}
public function mouseDownHandler(e:MouseEvent):void {
mousePressed = true;
}
public function mouseUpHandler(e:MouseEvent):void {
mousePressed = false;
}
public function removeUpgrade():void {
stage.removeChild(upgrade);
}
public function PlayershootBullet():void {
var bullet:Bullet = new Bullet(stage, player.x, player.y, player.rotation, false);
bullet.addEventListener(Event.REMOVED_FROM_STAGE, bulletRemoved, false, 0, true); //if the bullet goes off the stage
// we will call the bulletremoved function
bulletList.push(bullet); // add this bullet to the current list
stage.addChild(bullet);
}
public function bulletRemoved(e:Event):void {
e.currentTarget.removeEventListener(Event.REMOVED_FROM_STAGE, bulletRemoved); // remove the listener to stop errors
bulletList.splice(bulletList.indexOf(e.currentTarget), 1); // remove the bullet thats off the stage from the array
}
public function takeHit(wounded, shooter) {
wounded.life -= shooter.dmg;
wounded.healthratio = (wounded.life/wounded.maxLife) * 100;
trace(wounded.healthratio);
}
public function enemyFlash(flasher, shooter) {
flasher.alpha = 0;
if (hurt == false) {
takeHit(flasher, shooter);
}
}
public function hitReset(hostile) {
trace("hitReset");
hitTimer = 0;
losingHealth = false;
hurt = false;
hostile.alpha = 1;
}
public function removeEnemy(cya) {
deathTimer = 0;
cya.removeSelf();
for (var i = 0; i < bulletList.length;i++) {
bulletList[i].x = stage.stageWidth + 100;
}
}
public function collisionCheck(bullets, shooter, target) {
for (var i = 0; i < bullets.length; i++) {
if (bullets[i].hitTestObject(target)) {
//bulletList[i].has_hit = true;
bullets[i].x = stage.stageWidth + 100;
bullets[i].y = stage.stageHeight + 100;
losingHealth = true;
}
}
if (target.healthratio <= 0) {
removeEnemy(target);
}
}
public function healthLoss(shooter, target) {
hitTimer++;
if (hitTimer < 10) {
enemyFlash(target, shooter);
hurt = true;
}
else {
hitReset(target);
}
}
}
}
答案 0 :(得分:0)
很可能你的问题来自你用于所有事情的单个布尔值lossHealth。该布尔值只能有2个值,但是你可以将它用于所有敌人和玩家。单个布尔值不可能跟踪所有敌人和玩家状态。你应该拥有的是每个敌人和玩家的一个布尔值。