2016-06-17 22 views
0

Ich bin ein Tutorial zum Aufbau eines Rhythmus-Spiels in as3 here folgen, und ich bin sehr neu in der Sprache . Auf die swf läuft, ich folgende Fehlermeldung in der Ausgabe erhalten:as3 Fehler: ReferenceError: Fehler # 1069: Property keyCode nicht in flash.display.Shape gefunden und es gibt keinen Standardwert

ReferenceError: Error #1069: Property keyCode not found on flash.display.Shape and there is no default value. 
at source_fla::MainTimeline/makeLvl()[source_fla.MainTimeline::frame10:116] 

ich einige bisherigen Lösungen auf den gleichen Fehler geschrieben habe versucht, aber ich habe es nicht gelungen, das Problem zu beheben. Hier

ist der Quellcode:

stop(); 
stage.focus = stage; 
//VARIABLES 
//sTime is the current frame that is being played 
//Once it reaches sTempo, then it will be reset 
//and a note will be created 
var sTime:int = 0; 
//sTempo is how many frames it takes before 
//a note is created. Because it's 12, and 
//the frame rate is 24, it will take a half of a second 
//for a note to be made 
var sTempo:Number = 12; 
//sNote is the current arrow of the level that is created 
//0 makes no arrow 
//1 makes a left arrow 
//2 makes an up arrow 
//3 makes a down arrow 
//4 makes a right arrow 
var sArrow:int = 0; 
//arrowSpeed is how fast the arrow moves up the screen 
var arrowSpeed:Number = 10; 
//gameIsOver is whether the game's over 
var gameIsOver:Boolean = false; 
//the score variable 
var score:int = 0; 
//either perfect, great, nice, or good 
var scoreString:String = ''; 

var combo:int = 0; 

var mcHealth:Number = 0; 


//Booleans checking if the arrows are touching the receptor 
var touchLeft:Boolean = false; 
var touchUp:Boolean = false; 
var touchDown:Boolean = false; 
var touchRight:Boolean = false; 

function beginCode():void{ 
    addEventListener(Event.ENTER_FRAME, makeLvl); 

    //make the level array longer 
    lvlArrayAll[lvlCurrent].push(0,0,0,0,0); 
} 

function makeLvl(e:Event):void{ 
    //code here will create the level 
    if(sTime < sTempo){ 
     //if the required time hasn't reached the limit 
     //then update the time 
     sTime ++; 
    } else { 
     //if the time has reached the limit 
     //then reset the time 
     sTime = 0; 
     //if an actual arrow can be made 
     if(lvlArrayAll[lvlCurrent][sArrow] != 0){ 
      var currentArrow:MovieClip; //this will hold the current arrow 
      if(lvlArrayAll[lvlCurrent][sArrow] == 1){ 
       //place a left note onto the stage 
       currentArrow = new arrowLeft(); 
       //set the _x value of the note so that it is in the 
       //right place to touch the receptor 
       currentArrow.x = 105 ; 
       //set the note's y coordinate off of the stage 
       //so that the user can't see it when it appears 
       currentArrow.y = 0; 
       //setting the key that needs to be pressed 
       currentArrow.keyCode = 68; 
       addChild(currentArrow);//add it to stage 
      } else if(lvlArrayAll[lvlCurrent][sArrow] == 2){ 
       //place an up note onto the stage 
       currentArrow = new arrowUp(); 
       currentArrow.x = 230; 
       currentArrow.y = 0; 
       currentArrow.keyCode = 70; 
       addChild(currentArrow); 
      } else if(lvlArrayAll[lvlCurrent][sArrow] == 3){ 
       //place a down note onto the stage 
       currentArrow = new arrowDown(); 
       currentArrow.x = 355; 
       currentArrow.y = 0; 
       currentArrow.keyCode = 74; 
       addChild(currentArrow); 
      } else if(lvlArrayAll[lvlCurrent][sArrow] == 4){ 
       //place a right note onto the stage 
       currentArrow = new arrowRight(); 
       currentArrow.x = 480; 
       currentArrow.y = 0; 
       currentArrow.keyCode = 75; 
       addChild(currentArrow); 
      } 
     } 
     //get the next arrow if it the song isn't finished 
     if(sArrow < lvlArrayAll[lvlCurrent].length){ 
      sArrow ++; 
     } else { 
      //if the song is finished, then reset the game 
      gotoAndStop('win'); 
      gameIsOver = true; 
      //then remove this enter_frame listener 
      removeEventListener(Event.ENTER_FRAME, makeLvl); 
     } 
    } 

    //checking if mcReceptor is touching any arrows 
    //first we reset the variables we got last time just in case they aren't true anymore 
    touchLeft = false; 
    touchUp = false; 
    touchDown = false; 
    touchRight = false; 
    //this for loop will be used for the hit testing 
    for(var i:int = 0;i<numChildren;i++){ 
     var target:Object = getChildAt(i); 
     if(target.keyCode != null && target.hitTestObject(mcReceptor)){//if the object is an arrow and the receptor is touching it 
      if(target.keyCode == 68){//if left arrow 
       touchLeft = true; 
      } else if(target.keyCode == 70){//if up arrow 
       touchUp = true; 
      } else if(target.keyCode == 74){//if down arrow 
       touchDown = true; 
      } else if(target.keyCode == 75){//if right arrow 
       touchRight = true; 
      } 
     } 
    } 
    //changing the score text 
    mcTxt.txtScore.text = 'Score: '+score; 
    mcTxt.txtCombo.text = 'Combo: '+combo; 
    mcTxt.txtScoreString.text = scoreString; 
} 

//this function will change the health depending on how much health change 
//it needs, positive or negative 
function changeHealth(healthDiff:Number):void{ 
    healthDiff = 100;//only changes in percentages 
    //checking if the health is already at it's full 
    //or will be full after this hit 
    if(mcHealth + healthDiff >= 100){ 
     mcHealth = 100; 
    } else if(mcHealth + healthDiff <= 0){ 
     //checking if the health will be equal or below 0 
     gotoAndStop('lose'); 
     gameIsOver = true; 
     removeEventListener(Event.ENTER_FRAME, makeLvl); 
    } else { 
     //if there are no problems 
     mcHealth += healthDiff; 
    } 
} 

stage.addEventListener(KeyboardEvent.KEY_DOWN, checkKeys); 
function checkKeys(event:KeyboardEvent):void{ 
    //if the left key is down and no left arrows are touching the receptor 
    if(event.keyCode == 68 && !touchLeft){ 
     changeHealth(-10);//make the health go down 
     combo = 0; 
     scoreString = 'Bad'; 
    } else if(event.keyCode == 70 && !touchUp){//and so on 
     changeHealth(-10); 
     combo = 0; 
     scoreString = 'Bad'; 
    } else if(event.keyCode == 74 && !touchDown){ 
     changeHealth(-10); 
     combo = 0; 
     scoreString = 'Bad'; 
    } else if(event.keyCode == 75 && !touchRight){ 
     changeHealth(-10); 
     combo = 0; 
     scoreString = 'Bad'; 
    } 
} 

beginCode(); 

Kann mir jemand sagen, warum dieser Fehler auftritt? Vielen Dank.

Antwort

2

Beim Iterieren numChildren ist es notwendig zu überprüfen, ob das Objekt ein Pfeil ist oder nicht.
Vielleicht können Sie es unterscheiden, indem Sie keyCode Eigenschaft haben oder nicht.

Versuchen Sie, Object.hasOwnProperty(property name) Methode zu verwenden.

if (target.hasOwnProperty("keyCode")){ 
    // access target.keyCode here. 
} 

Oder das könnte auch funktionieren.

if (target is arrowLeft || target is arrowUp || target is arrowDown || target is arrowRight){ 
    // the target should be arrow class 
    // access target.keyCode here. 
} 

//this for loop will be used for the hit testing 
for(var i:int = 0;i<numChildren;i++){ 
    var target:Object = getChildAt(i); 

    if (target.hasOwnProperty("keyCode")){  // If the object is an arrow, that object should has keyCode property. 

     if(target.keyCode != null && target.hitTestObject(mcReceptor)){//if the object is an arrow and the receptor is touching it 
      if(target.keyCode == 68){//if left arrow 
       touchLeft = true; 
      } else if(target.keyCode == 70){//if up arrow 
       touchUp = true; 
      } else if(target.keyCode == 74){//if down arrow 
       touchDown = true; 
      } else if(target.keyCode == 75){//if right arrow 
       touchRight = true; 
      } 
     } 
    } 
}