2013-03-20 2 views
0

Это класс Хели, и он будет вызываться в основном классе, цель основного класса в кадре 2. В кадре 1 есть кнопка, которая может заставить меня перейти к кадру 2. Но этот мувиклип Хели не добавляется на сцену. но он будет работать, если я нацелился на основной класс в кадре 1. так может ли кто-нибудь сказать мне, как использовать событие added_to_stage в указанном кадре ??? (Жаль мой английский)ADDED_TO_STAGE Событие не работает в определенном кадре?

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 

    }  
} 
} 

ответ

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

Я извиняюсь, но, S-прежнему не работает. Я хочу, чтобы этот класс вызывался, когда я нажал определенную кнопку («перейдите к кадру 2»). –

0

здесь главный класс

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; 
     } 

    }  

} 

}