我刚开始用as3编码。 现在我正在尝试制作一个玩家只需要避开触摸迷宫内的敌人的游戏。 我设法让玩家和敌人之间以及玩家和墙壁之间发生碰撞。在此之前一切正常。
我面临的问题是: 我无法让敌人独立移动并在与墙壁碰撞后改变他们的动作(方向)。
我试图为墙壁制作一个for循环,并在其中为敌人制作另一个for循环。但所有的敌人似乎都是一体的。当其中一个敌人(来自enemiesArray)与其中一个墙壁(来自wallAray)发生碰撞时,所有敌人都会旋转并向所有方向移动。
我需要的是让敌人单独移动和旋转......
我只是箍有人可以告诉我在我的代码中要改变它的工作原理
我一直在寻找一个左右的解决方案...
这就是我编写代码的方式: 我希望它不会太乱(我还有很多东西需要学习)。
import flash.display.MovieClip;
import flash.text.TextField;
import flash.media.SoundChannel;
import flash.events.TimerEvent;
//variables
var stageVar:Stage = stage;
var player:MovieClip = player;
var playerAni:MovieClip = player.playerAni;
var enemyAni:MovieClip = enemy0.enemyAni;
var StartWindow:MovieClip = StartWindow;
var gameOverWindow:MovieClip = gameOverWindow;
var startButton:MovieClip = startButton;
var playAgainButton:MovieClip = gameOverWindow.playAgainButton;
var score:int;
var life:int = 3;
var scoreBoard:TextField = scoreBoard;
var lifeBoard:TextField = lifeBoard;
var key0:MovieClip = key0;
var key1:MovieClip = key1;
var key2:MovieClip = key2;
var key3:MovieClip = key3;
var keyIn0:MovieClip = key0.keyIn0;
var keyIn1:MovieClip = key1.keyIn1;
var keyIn2:MovieClip = key2.keyIn2;
var keyIn3:MovieClip = key3.keyIn3;
var gameTimer:Timer = new Timer(0);
var gameOverTimer:Timer = new Timer(0);
var eDirArray:Array = new Array("LEFT","RIGHT","UP","DOWN");
var enemiesArray:Array = new Array(enemy0,
enemy1,enemy2,enemy3,enemy4,
enemy5,enemy6,enemy7,enemy8);
var wallArray:Array = new Array(wall0,
wall1,wall2,wall3,wall4,wall5);
//init
stageVar.frameRate = 31;
playerAni.stop();
gameTimer.delay = 5;
gameTimer.repeatCount = 0;
gameTimer.start();
gameOverWindow.visible = false;
StartWindow.visible = true;
startButton.visible = true;
startButton.buttonMode = true;
playAgainButton.buttonMode = true;
//COUNT DOWN
var nCount:int = 3;
var nCount2:int = 3;
var countDouwnTimer:Timer = new Timer(1000, nCount);
var countDouwnTimer2:Timer = new Timer(1000, nCount2);
countDouwnTimer.addEventListener(TimerEvent.TIMER, countdown);
countDouwnTimer2.addEventListener(TimerEvent.TIMER, countdown);
function countdown(e:TimerEvent):void {
nCount--;
countDownNr.text2.text = nCount.toString();
countDownNr.text1.text = nCount.toString();
if(nCount<1){
countDownNr.text1.text = "RUN";
countDownNr.text2.text = "RUN";
countDouwnTimer2.start();
nCount2--;
}
if(nCount2<1){
countDownNr.text1.text = "";
countDownNr.text2.text = "";
countDouwnTimer.removeEventListener(TimerEvent.TIMER, countdown);
countDouwnTimer2.removeEventListener(TimerEvent.TIMER, countdown);
}
}
//RESET / START PLAYER'S POSITION
function playerStartPos(){
player.x = 43.75;
player.y = 54.05;
life -=1;
lifeBoard.text = life.toString();
}
// PLAYER MOVEMENTS
//*******************************************
var pDir:String = new String();
// Events
stageVar.addEventListener(KeyboardEvent.KEY_DOWN,keyPressedDown);
stageVar.addEventListener(KeyboardEvent.KEY_UP,keyPressedUp);
//Keys not being pressed
function keyPressedUp(e:KeyboardEvent):void {
pDir = "NONE";
playerAni.stop();
}
//Keys being pressed Down
function keyPressedDown(e:KeyboardEvent){
playerAni.play();
var key:uint=e.keyCode;
if(key == Keyboard.LEFT){
pDir="LEFT";
}
if(key == Keyboard.RIGHT){
pDir = "RIGHT";
}
if(key == Keyboard.UP){
pDir = "UP";
}
if(key == Keyboard.DOWN){
pDir = "DOWN";
}
}
//Move the player
function movePlayer(){
if(pDir=="LEFT"){
player.x -= 1;
}
if(pDir=="RIGHT"){
player.x += 1;
}
if(pDir=="UP"){
player.y -= 1;
}
if(pDir=="DOWN"){
player.y += 1;
}
}
//Rotate the player
function rotatePlayer(){
if(pDir=="LEFT"){
player.rotation= -180;
}
if(pDir=="RIGHT"){
player.rotation=0;
}
if(pDir=="UP"){
player.rotation=-90;
}
if(pDir=="DOWN"){
player.rotation=90;
}
}
//ENEMY MOVEMENTS
//*****************************************************
//Move the enemy up, down, left and right
var eDir:String = new String();
var enemySpeed:Number = 1/2;
eDir = "RIGHT";
function moveEnemy(){
for(var i:uint = 0; i < enemiesArray.length;i++){
var enemy:MovieClip = enemiesArray[i];
if(eDir=="LEFT"){
enemy.x -= enemySpeed;
}
if(eDir=="RIGHT"){
enemy.x += enemySpeed;
}
if(eDir=="UP"){
enemy.y -= enemySpeed;
}
if(eDir=="DOWN"){
enemy.y += enemySpeed;
}
}
}
//Rotate enemy
function rotateEnemy(){
for(var i:uint = 0; i < enemiesArray.length;i++){
var enemy:MovieClip = enemiesArray[i];
if(eDir=="LEFT"){
enemy.rotation= -180;
}
if(eDir=="RIGHT"){
enemy.rotation=0;
}
if(eDir=="UP"){
enemy.rotation=-90;
}
if(eDir=="DOWN"){
enemy.rotation= 90;
}
}
}
//COLLISIONS
//**************************************************
//Player collision with the walls
function playerWallCollision(){
for(var i:uint = 0; i < wallArray.length;i++){
var wall:MovieClip = wallArray[i];
if(player.hitTestObject(wall)){
if (pDir == "LEFT"){
player.x += 1;
}
if (pDir == "RIGHT"){
player.x -= 1;
}
if (pDir == "UP"){
player.y += 1;
}
if (pDir == "DOWN"){
player.y -= 1;
}
}
}
}
这是我被卡住的地方......我不知道如何让敌人在碰撞后独立移动和旋转。我从这段代码得到的是:它们在碰撞后旋转并移开但是它们一起移动,并且因为它们响应彼此的碰撞,所以每次发生碰撞时它们都会保持碰撞和旋转。
//Enemy Collision with the walls (NOT WORKING)
var nr:uint = Math.round(Math.random()*3);
function enemyWallCollision(){
for(var i:uint = 0; i < enemiesArray.length;i++){
var enemy:MovieClip = enemiesArray[i];
for(var j:uint = 0; j < wallArray.length;j++){
var wall:MovieClip = wallArray[j];
if(enemy.hitTestObject(wall)){
if (eDir == "LEFT"){
enemy.x += enemySpeed;
}
if (eDir == "RIGHT"){
enemy.x -= enemySpeed;
trace("hopa");
}
if (eDir == "UP"){
enemy.y += enemySpeed;
}
if (eDir == "DOWN"){
enemy.y -=enemySpeed;
}
//break;
var nr:uint = Math.round(Math.random()*3);
eDir = eDirArray[nr];
}
}
}
}
这是代码的其余部分......
//Player collision with enemy
function playerEnemyCollision(){
for(var i:uint = 0; i < enemiesArray.length;i++){
var enemy:MovieClip = enemiesArray[i];
if(player.hitTestObject(enemy) == true){
playerStartPos();
playBustedSound();
}
}
}
//Player collect keys
var keysArray:Array = new Array(key0,
key1,key2,key3,key4,key5);
var colectedKeys:TextField = colectedKeys;
var keys:int = 0;
// The start position of the keys at the beginning of the game
function keyPos(){
key0.x = 20;
key1.x = 191;
key2.x = 295;
key3.x = 272;
key4.x = 430;
key5.x = 489;
}
//Player collision with the key (this allows the player to get the key)
function keyCapture(){
for(var i:uint = 0; i < keysArray.length;i++){
var key:MovieClip = keysArray[i];
// if the player dies set all the keys back in the game
if (life<1){
keyPos();
keys = 0;
}
// If the player touches the key, encrease the nr of collected keys by 1
// Send that to the textfield
//Play the a sound
// Put the key 20px out of the stage
if(player.hitTestObject(key) == true){
keys+=1;
colectedKeys.text = keys.toString();
playKeySound();
key.x = stage.stageWidth + 20;
}
//When all the keys are collected the textfield displays "Complete"
if(keys==6){
colectedKeys.text = "Complete";
}
}
}
// OTHER FUNCTIONS
//SOUNDS
//********************************************
var startSoundPosition:Number = 0.00;
var isPlayingStart:Boolean;
var startSoundChanel:SoundChannel = new SoundChannel();
var startSound:Sound = new Sound(new URLRequest("startGameSound.mp3"));
var gameOverSoundChanel:SoundChannel = new SoundChannel();
var gameOverSound:Sound = new Sound(new URLRequest("gameOverSound.mp3"));
var bustedSoundChanel:SoundChannel = new SoundChannel();
var bustedSound:Sound = new Sound(new URLRequest("bustedSound.mp3"));
var gameSoundChanel:SoundChannel = new SoundChannel();
var gameSound:Sound = new Sound(new URLRequest("gameSound.mp3"));
var keySoundChanel:SoundChannel = new SoundChannel();
var keySound:Sound = new Sound(new URLRequest("collectKeySound.mp3"));
// play sound at the start
function playStartSound() {
if (isPlayingStart) {
startSoundPosition = startSoundChanel.position;
startSoundChanel.stop();
isPlayingStart = false;
}
else if (!isPlayingStart) {
startSoundChanel = startSound.play(startSoundPosition);
isPlayingStart = true;
}
}
playStartSound();
// Play a sound during the game
function playGameSound(){
gameSoundChanel = gameSound.play();
}
//This function Plays sound when the player touches the enemy
function playBustedSound(){
bustedSoundChanel = bustedSound.play();
}
//This function plays a sound when the player gets a key
function playKeySound(){
keySoundChanel = keySound.play();
}
// This function plays a sound when the game is over
function playGameOverSound(){
gameOverSoundChanel = gameOverSound.play();
}
// BUTTONS
//************************************************
//Start game function
startButton.addEventListener(MouseEvent.CLICK,startGame);
function startGame(event:MouseEvent):void{
countDouwnTimer.start();
countDownNr.text1.text = nCount.toString();
countDownNr.text2.text = nCount.toString();
playGameSound();
life = 3;
lifeBoard.text = life.toString();
lifeBoard.text = "3";
keys = 0;
colectedKeys.text = keys.toString();
StartWindow.visible = false;
startButton.visible = false;
gameOverWindow.visible = false;
startSoundChanel.stop();
gameOverTimer.start();
gameTimer.addEventListener(TimerEvent.TIMER,onStartUp);
gameOverTimer.addEventListener(TimerEvent.TIMER,onGameOver);
}
//Play again Function
playAgainButton.addEventListener(MouseEvent.CLICK,playAgain);
function playAgain(event:MouseEvent){
playGameSound();
life = 3;
lifeBoard.text = life.toString();
lifeBoard.text = "3";
colectedKeys.text = keys.toString();
StartWindow.visible = false;
startButton.visible = false;
gameOverWindow.visible = false;
gameOverSoundChanel.stop();
gameOverTimer.start();
gameTimer.addEventListener(TimerEvent.TIMER,onStartUp);
gameOverTimer.addEventListener(TimerEvent.TIMER,onGameOver);
}
//GAME OVER
//*************************************************
gameOverTimer.delay = 1;
gameOverTimer.repeatCount = 0;
gameOverTimer.stop();
function gameOver(){
gameOverWindow.visible=true;
Mouse.show();
playGameOverSound();
gameSoundChanel.stop();
}
function onGameOver (e:TimerEvent):void{
if(life==0){
gameOver();
gameOverTimer.stop();
}
}
//AT THE START - TIMER
//****************************************************
function onStartUp(e:TimerEvent):void{
keyCapture();
moveEnemy();
rotateEnemy();
enemyWallCollision();
movePlayer();
rotatePlayer();
playerWallCollision();
playerEnemyCollision();
}