문제

I need advice, please. I'm working on one project - a simple game. It will be something like "Space Invaders". I just needed to cater to the ship could not leave the area (Stage). Function, is called "RMimoXY" does not work. Could someone please check out what I'm missing in the program?

Thanks in advance for your advice.

import flash.events.KeyboardEvent;
import flash.display.Sprite;
import flash.display.DisplayObject;
import flash.media.Sound;
import flash.display.Stage;

var let: Boolean = false;
var pozadi: Stage;
var vx:Number = 0;
var vy:Number = 0;

function mezernik(){
    var mySound: Sound = new laserFire(); 
    mySound.play();
    RMimoXY();
}

stage.addEventListener(KeyboardEvent.KEY_DOWN, klavesnice);

function klavesnice(e: KeyboardEvent){
    switch(e.keyCode){
        case Keyboard.LEFT: lod.x += -5; break;
        case Keyboard.RIGHT: lod.x += 5; break;
        case Keyboard.UP: lod.y += -5; break;
        case Keyboard.DOWN: lod.y += 5; break;
        case Keyboard.SPACE: mezernik(); break;
    }
}

function RMimoXY(){
    if (lod.x > stage.stageWidth ){ 
        lod.x =  0 - lod.width; 
    } 
    else if (lod.x < 0 - lod.width ){ 
        lod.x = stage.stageWidth; 
    } 
    if (lod.y > stage.stageHeight ){ 
        lod.y = 0 - lod.height; 
    } 
    else if (lod.y < 0 - lod.height ){ 
        lod.y = stage.stageHeight; 
    }
}
도움이 되었습니까?

해결책

It appears as if you're only calling RMimoXY in your constructor. You should call it every time the ship is moved. So adding it to the end of your keyhandler should work:

function klavesnice(e: KeyboardEvent){
    switch(e.keyCode){
        case Keyboard.LEFT: lod.x += -5; break;
        case Keyboard.RIGHT: lod.x += 5; break;
        case Keyboard.UP: lod.y += -5; break;
        case Keyboard.DOWN: lod.y += 5; break;
        case Keyboard.SPACE: mezernik(); break;
    }
    RMimoXY();
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top