2013-04-11 55 views
0

这里是错误>> TypeError:错误#1034:类型强制失败:无法转换flash.display: :将@ 261b4089展示给flash.display.MovieClip。 在com.ply ::合力/ fireBullet() 在com.ply ::合力/ myOnPress()错误#1034:类型强制失败:无法将flash.display :: Stage @ 27dfe089转换为flash.display.MovieClip

这是直升机的类别:

package com.ply 
{ 
import flash.display.MovieClip; 
import flash.display.*; 
import flash.events.Event; 
import flash.events.KeyboardEvent; 


import flash.ui.Keyboard; 
import com.peluru.Bullet; 

public class Heli extends MovieClip 
{ 
    var shotCooldown:int; 
    const MAX_COOLDOWN = 10; 
    //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; 

    public function Heli() 
    { 
     shotCooldown = MAX_COOLDOWN; 
     bullets = new Array(); 
     addEventListener(Event.ENTER_FRAME, update); 
     addEventListener(Event.ADDED_TO_STAGE, onAddedToStage); 
     init(); 
    } 

    public function onAddedToStage(event:Event):void 
    { 
     removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage); 
     stage.addEventListener(KeyboardEvent.KEY_DOWN, myOnPress); 
     stage.addEventListener(KeyboardEvent.KEY_UP, myOnRelease); 
     init(); 
    } 


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

    private function RunHeli(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 update(e:Event){ 
    shotCooldown-- ; 
    } 

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

    public function myOnRelease(event:KeyboardEvent):void 
    { 

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

    }  

    public function fireBullet() { 
     if (shotCooldown <=0) 
     { 
      shotCooldown=MAX_COOLDOWN; 
      var b = new Bullet(); 
      var b2= new Bullet(); 
      b.x = this.x +20; 
      b.y = this.y ; 
      b2.x= this.x -20; 
      b2.y= this.y ; 
      MovieClip(parent).bullets.push(b); 
      MovieClip(parent).bullets.push(b2); 
      trace(bullets.length); 
      parent.addChild(b); 
      parent.addChild(b2); 
     } 
    } 

} 

}

,这是在主类

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.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(); 
    //================================================ 
    private var nextPlane:Timer; 
    public var enemies:Array; 
    public var bullets:Array; 

    public function Main() 
    { 
     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 { 
     enemies = new Array(); 
     bullets = ne Array(); 

     setNextPlane(); 

     gameState = STATE_START_PLAYER; 
     gameLoop(); 
     //stage.addEventListener(Event.ENTER_FRAME, AddEnemy); 
     stage.addEventListener(Event.ENTER_FRAME, back); 
     stage.addEventListener(Event.ENTER_FRAME,checkForHits); 
    } 

    public function setNextPlane() { 
     nextPlane = new Timer(1000+Math.random()*1000,1); 
     nextPlane.addEventListener(TimerEvent.TIMER_COMPLETE,newPlane); 
     nextPlane.start(); 
    } 


    public function newPlane(event:TimerEvent) { 
     // random side, speed and altitude 

     // create plane 
     var enemy:Airplane2 = new Airplane2(); 
     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); 
     enemies.push(enemy); 

     // set time for next plane 
     setNextPlane(); 
    } 

    public function removePlane(plane:Airplane2) { 
     for(var i in enemies) { 
      if (enemies[i] == plane) { 
       enemies.splice(i,1); 
       break; 
      } 
     } 
    } 

    // take a bullet from the array 
    public function removeBullet(bullet:Bullet) { 
     for(var i in bullets) { 
      if (bullets[i] == bullet) { 
       bullets.splice(i,1); 
       break; 
      } 
     } 
    } 

    public function checkForHits(event:Event) { 
     for(var bulletNum:int=bullets.length-1;bulletNum>=0;bulletNum--){ 
      for (var airplaneNum:int=enemies.length-1;airplaneNum>=0;airplaneNum--) { 
       if (bullets[bulletNum].hitTestObject(enemies[airplaneNum])) { 
        enemies[airplaneNum].planeHit(); 
        bullets[bulletNum].deleteBullet(); 
        trace("kena"); 
        //shotsHit++; 
        //showGameScore(); 
        break; 
       } 
      } 
     } 

     //if ((shotsLeft == 0) && (bullets.length == 0)) { 
     // endGame(); 
     //} 
    } 


    public function back(evt:Event):void 
    { // Backgroud Land 
     if (Dessert2.y >= 0 && Dessert.y >= 720) 
     { 
      Dessert.y = Dessert2.y - 1240 ; 
     } 
     else if (Dessert.y >= 0 && Dessert2.y >= 720) 
     { 
      Dessert2.y = Dessert.y - 1240 ; 
     } 
     else 
     { 
     Dessert.y = Dessert.y +5 ; 
     Dessert2.y = Dessert2.y +5 ; 
     } 
     // Background Clouds 
     if (Clouds2.y >= 0 && Clouds.y >= 720) 
     { 
      Clouds.y = Clouds2.y - 2480 ; 
     } 
     else if (Clouds.y >= 0 && Clouds2.y >= 720) 
     { 
      Clouds2.y = Clouds.y - 2480 ; 
     } 
     else 
     { 
     Clouds.y = Clouds.y +10 ; 
     Clouds2.y = Clouds2.y +10 ; 
     } 
    } 

    public function startPlayer() : void { 
     player=new Heli(); 
     player.x = stage.stageWidth/2; 
     player.y = stage.stageHeight/2; 
     // add Player to display list 
     stage.addChild(player); 
     gameState = STATE_PLAY_GAME; 

    } 

    public function playGame():void { 

     gameLoop(); 

     //txtScore.text = 'Score: '+score; 


    } 

    function AddEnemy(event:Event){ 
    if(enemyTime < enemyLimit){ 
     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; 
     } 

    } 

} 

}

+0

只有一个'ENTER_FRAME'事件的使用是一个很好的pactice。在单个“ENTER_FRAME”事件中使用ON/OFF开关作为对象。 – 2013-04-12 19:53:27

回答

0

您已将本player in Main into stage而不是this - 顺便说一句,为什么?所以,有两种方法可以解决这个问题:首先,将player添加到显示列表中,然后从Heli.fireBullet()函数中删除直接强制。我会说使用第一个。

public function startPlayer() : void { 
    player=new Heli(); 
    player.x = stage.stageWidth/2; 
    player.y = stage.stageHeight/2; 
    // add Player to display list 
    this.addChild(player); // <-- this, not stage 
    gameState = STATE_PLAY_GAME; 

} 
0

变化

MovieClip(parent).bullets.push(b); 
MovieClip(parent).bullets.push(b2); 

MovieClip(root).bullets.push(b); 
MovieClip(root).bullets.push(b2); 
+1

我建议不要依赖'root',这是IIRC不推荐使用的属性。 – Vesper 2013-04-11 13:59:29

相关问题