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