简体   繁体   English

ADDED_TO_STAGE事件在特定帧中不起作用?

[英]ADDED_TO_STAGE Event doesn't work in certain frame?

This is Heli's Class and it'll called in main class ,the target of main class in frame 2 . 这是Heli的班级,将在主班级中调用,第2帧中主班级的目标。 The frame 1 contain button that can make me go to frame 2 .but this movie clip of Heli doesn't added to the stage . 第1帧包含可以使我转到第2帧的按钮。但是Heli的此影片剪辑没有添加到舞台上。 but it'll work if i targeting main class in frame 1 . 但是如果我在第1帧中定位主类,它将起作用。 so can anyone tell me how to use added_to_stage event in specified frame ??? 所以谁能告诉我如何在指定的帧中使用added_to_stage事件? (sorry about my english) (对不起我的英语)

package com.ply 
{
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent; 
import flash.ui.Keyboard;

public class Heli extends MovieClip
{
    //Settings
    public var xAcceleration:Number = 0;
    public var yAcceleration:Number = 0;
    private var xSpeed:Number = 0;
    private var ySpeed:Number = 0;

    private var up:Boolean = false;
    private var down:Boolean = false;
    private var left:Boolean = false;
    private var right:Boolean = false;

    private var bullets:Array;
    private var missiles:Array;


    public function Heli()
    {
        addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
    }

    public function onAddedToStage(event:Event):void 
    {
        removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);

        init();
    }


    private function init():void
    {
        stage.addEventListener(Event.ENTER_FRAME, runGame);         
    }

    private function runGame(event:Event):void
    {
        xSpeed += xAcceleration ;       //increase the speed by the acceleration
        ySpeed += yAcceleration ;       //increase the speed by the acceleration

        xSpeed *= 0.95;                 //apply friction
        ySpeed *= 0.95;                 //so the speed lowers after time

        if(Math.abs(xSpeed) < 0.02)     //if the speed is really low
        {
            xSpeed = 0;                 //set it to 0
                                //Otherwise I'd go very small but never really 0
        }
        if(Math.abs(ySpeed) < 0.02)     //same for the y speed
        {
            ySpeed = 0;
        }

        xSpeed = Math.max(Math.min(xSpeed, 10), -10);       //dont let the speed get bigger as 10
        ySpeed = Math.max(Math.min(ySpeed, 10), -10);       //and dont let it get lower than -10

        this.x += xSpeed;               //increase the position by the speed
        this.y += ySpeed;               //idem

    }       
}   
}
public function Heli()
{
    if (stage) init();
    addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}

here it is the main class 这是主班

package  
{
import com.ply.Heli;
import com.peluru.Bullet;
import com.musuh.Airplane2;
import flash.display.Sprite;
import flash.display.Stage;
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;

import flash.events.Event;
import flash.events.MouseEvent;
import flash.display.*;
import flash.events.*;
import flash.utils.*;

public class Main extends MovieClip
{
    public static const STATE_INIT:int = 10;
    public static const STATE_START_PLAYER:int = 20;
    public static const STATE_PLAY_GAME:int = 30;
    public static const STATE_REMOVE_PLAYER:int = 40;
    public static const STATE_END_GAME:int = 50;        

    public var gameState:int = 0;
    public var player:Heli;
    public var enemy:Airplane2;
    //public var bulletholder:MovieClip = new MovieClip();

    //================================================
    public var cTime:int = 1;
    //the time it has to reach in order to be allowed to shoot (in frames)
    public var cLimit:int = 10;
    //whether or not the user is allowed to shoot
    public var shootAllow:Boolean = true;
    //how much time before another enemy is made
    public var enemyTime:int = 0;
    //how much time needed to make an enemy
    //it should be more than the shooting rate
    //or else killing all of the enemies would
    //be impossible :O
    public var enemyLimit:int = 64;
    //the player's score
    public var score:int = 0;
    public var gameOver:Boolean = false;
    public var bulletContainer:MovieClip = new MovieClip();
    //================================================

    public function Main() 
    {
        //stage.addEventListener(Event.ENTER_FRAME, gameLoop);
        // instantiate car class
        gameState = STATE_INIT;
        gameLoop();
    }


    public function gameLoop(): void {
        switch(gameState) {
            case STATE_INIT :
                initGame();
                break
            case STATE_START_PLAYER:
                startPlayer();
                break;  
            case STATE_PLAY_GAME:
                 playGame();
                break;
            case STATE_REMOVE_PLAYER:
                //removePlayer();
                break;      
            case STATE_END_GAME:                    
                break;  

        }

    }       

    public function initGame() :void {

        //addChild(bulletholder);
        addChild(bulletContainer);

        gameState = STATE_START_PLAYER;
        gameLoop();

        stage.addEventListener(KeyboardEvent.KEY_DOWN, myOnPress);
        stage.addEventListener(KeyboardEvent.KEY_UP, myOnRelease);
        stage.addEventListener(Event.ENTER_FRAME, AddEnemy);
    }

    public function startPlayer() : void { 
        player=new Heli();

        player.x = stage.stageWidth / 2;
        player.y = stage.stageHeight / 2;
        // add car to display list
        stage.addChild(player);
        gameState = STATE_PLAY_GAME;

    }

    public function playGame():void {
        //CheckTime();

        gameLoop();
        //txtScore.text = 'Score: '+score;


    }

    public function myOnPress(event:KeyboardEvent):void
    {
        if(event.keyCode == Keyboard.LEFT)
        {
            player.xAcceleration  = -1;
        }
        else if(event.keyCode == Keyboard.RIGHT)
        {
            player.xAcceleration  = 1;
        }
        else if(event.keyCode == Keyboard.UP)
        {
            player.yAcceleration  = -1;
        }
        else if(event.keyCode == Keyboard.DOWN)
        {
            player.yAcceleration  = 1;
        }
        else if (event.keyCode == 32 && shootAllow)
        {
            fireBullet();   
        }
    }

    public function fireBullet() {
        //making it so the user can't shoot for a bit
        shootAllow = false;
        //declaring a variable to be a new Bullet
        var newBullet:Bullet = new Bullet();
        var newBullet2:Bullet = new Bullet();
        //changing the bullet's coordinates
        newBullet.x = player.x+20; //+ player.width/2 - player.width/2;
        newBullet.y = player.y;
        newBullet2.x = player.x-20;
        newBullet2.y = player.y;
        //then we add the bullet to stage
        bulletContainer.addChild(newBullet);
        bulletContainer.addChild(newBullet2);

    }

    function AddEnemy(event:Event){
    if(enemyTime < enemyLimit){
        //if time hasn't reached the limit, then just increment
        enemyTime ++;
    }   else {
        //defining a variable which will hold the new enemy
        enemy =new Airplane2();
        //making the enemy offstage when it is created
        enemy.y = -1 * enemy.height;
        //making the enemy's x coordinates random
        //the "int" function will act the same as Math.floor but a bit faster
        enemy.x = Math.floor(Math.random()*(stage.stageWidth - enemy.width));
        //then add the enemy to stage
        addChild(enemy);
        //and reset the enemyTime
        enemyTime = 0;
        }

    if(cTime <= cLimit){
            cTime ++;
        } else {
        //if it has, then allow the user to shoot
        shootAllow = true;
        //and reset cTime
        cTime = 1;
        }

    }

    public function myOnRelease(event:KeyboardEvent):void
    {

        if(event.keyCode == Keyboard.LEFT || event.keyCode == Keyboard.RIGHT)
        {
            player.xAcceleration  = 0;
        }
        else if(event.keyCode == Keyboard.UP || event.keyCode == Keyboard.DOWN)
        {
            player.yAcceleration  = 0;
        }

    }       

}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM