as3错误:ReferenceError:错误#1069:在flash.display.Shape上找不到属性keyCode,并且没有默认值 [英] as3 error: ReferenceError: Error #1069: Property keyCode not found on flash.display.Shape and there is no default value
问题描述
我正在学习如何在as3中构建一个节奏游戏的教程。这里,我对这个语言很陌生。在运行swf时,输出中出现以下错误:
ReferenceError:错误#1069:在flash上找不到属性keyCode .display.Shape并没有默认值。
at source_fla :: MainTimeline / makeLvl()[source_fla.MainTimeline :: frame10:116]
<我已经尝试了一些以前的解决方案张贴到相同的错误,但我一直无法解决这个问题。
这里是源代码:
stop();
stage.focus = stage;
// VARIABLES
// sTime是正在播放的当前帧
//一旦达到sTempo,它将被重置
//并且将创建一个音符
var sTime:int = 0;
// sTempo是在
//创建音符前需要多少帧。因为它是12,
//帧频是24,所以需要花费一半的时间
来创建一个音符
var sTempo:Number = 12;
// sNote是创建的级别的当前箭头
// 0不使用箭头
// 1使左箭头
// 2使得向上箭头
// 3向下箭头
// 4向右箭头
var sArrow:int = 0;
// arrowSpeed是箭头在屏幕上移动的速度
var arrowSpeed:Number = 10;
// gameIsOver是游戏是否超过
var gameIsOver:Boolean = false;
//得分变量
var score:int = 0;
//完美,好,好,好
var scoreString:String ='';
var combo:int = 0;
var mcHealth:Number = 0;
//布尔值检查箭头是否触及到接收器
var touchLeft:Boolean = false;
var touchUp:Boolean = false;
var touchDown:Boolean = false;
var touchRight:Boolean = false;
函数beginCode():void {
addEventListener(Event.ENTER_FRAME,makeLvl);
//使水平数组变长
lvlArrayAll [lvlCurrent] .push(0,0,0,0,0);
$ b $函数makeLvl(e:Event):void {
//这里的代码将创建级别
if(sTime< sTempo){
//如果所需时间未达到限制
//则更新时间
sTime ++;
} else {
//如果时间达到限制
//然后重置时间
sTime = 0;
//如果一个实际的箭头可以被创建
if(lvlArrayAll [lvlCurrent] [sArrow]!= 0){
var currentArrow:MovieClip; //如果(lvlArrayAll [lvlCurrent] [sArrow] == 1){
//将一个左音符放到舞台上
currentArrow = new arrowLeft();
//设置音符的_x值,使其位于
//正确的位置来触碰音色
currentArrow.x = 105;
//将音符的y坐标设置为离开舞台
//以便用户看不到它
currentArrow.y = 0;
//设置需要按的键
currentArrow.keyCode = 68;
addChild(currentArrow); //将它添加到舞台
} else if(lvlArrayAll [lvlCurrent] [sArrow] == 2){
//在舞台$ b上放置音符$ b currentArrow = new arrowUp();
currentArrow.x = 230;
currentArrow.y = 0;
currentArrow.keyCode = 70;
addChild(currentArrow);
} else if(lvlArrayAll [lvlCurrent] [sArrow] == 3){
//在舞台上放置一个音符
currentArrow = new arrowDown();
currentArrow.x = 355;
currentArrow.y = 0;
currentArrow.keyCode = 74;
addChild(currentArrow);
} else if(lvlArrayAll [lvlCurrent] [sArrow] == 4){
//在舞台上放置一个正确的音符
currentArrow = new arrowRight();
currentArrow.x = 480;
currentArrow.y = 0;
currentArrow.keyCode = 75;
addChild(currentArrow);
//如果歌曲未完成,则获取下一个箭头
if(sArrow< lvlArrayAll [lvlCurrent] .length){
sArrow ++;
} else {
//如果歌曲结束,则重置游戏
gotoAndStop('win');
gameIsOver = true;
//然后删除这个enter_frame监听器
removeEventListener(Event.ENTER_FRAME,makeLvl);
//检查mcReceptor是否触摸任何箭头
//首先我们重置上次获得的变量,以防万一它们不是真的不再
touchLeft = false;
touchUp = false;
touchDown = false;
touchRight = false;
// this for循环将用于命中测试
for(var i:int = 0; i< numChildren; i ++){
var target:Object = getChildAt(i);
if(target.keyCode!= null&& target.hitTestObject(mcReceptor)){//如果对象是一个箭头并且接受器正在触摸它
if(target.keyCode == 68 ){//如果向左箭头
touchLeft = true;
} else if(target.keyCode == 70){// if arrow arrow
touchUp = true;
} else if(target.keyCode == 74){// if down arrow
touchDown = true;
} else if(target.keyCode == 75){// if right arrow
touchRight = true;
}
}
}
//改变分数文字
mcTxt.txtScore.text ='Score:'+ score;
mcTxt.txtCombo.text ='Combo:'+ combo;
mcTxt.txtScoreString.text = scoreString;
}
//这个函数会根据健康状况的变化改变健康状况
//它需要,正面或负面
function changeHealth(healthDiff:Number) :void {
healthDiff = 100; //只更改百分比
//检查运行状况是否已满
//或在此命中后将满
if (mcHealth + healthDiff> = 100){
mcHealth = 100;
} else if(mcHealth + healthDiff <= 0){
//检查健康是否等于或低于0
gotoAndStop('lose');
gameIsOver = true;
removeEventListener(Event.ENTER_FRAME,makeLvl);
} else {
//如果没有问题
mcHealth + = healthDiff;
stage.addEventListener(KeyboardEvent.KEY_DOWN,checkKeys);
函数checkKeys(event:KeyboardEvent):void {
//如果左键关闭,没有左箭头触及接收器
if(event.keyCode == 68&& !touchLeft){
changeHealth(-10); //使健康状态下降
combo = 0;
scoreString ='Bad';
} else if(event.keyCode == 70&&!touchUp){//等等
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();
有人能告诉我为什么会出现这个错误吗?谢谢。
numChildren
时,需要检查对象是是否可以通过 keyCode
属性区分它。 尝试使用 Object.hasOwnProperty(属性名称)
方法。
if(target.hasOwnProperty(keyCode)){
//在这里访问target.keyCode。
}
或者这也可以。
if(target是arrowLeft || target是arrowUp || target是arrowDown || target是arrowRight){
//目标应该是arrow class
//在这里访问target.keyCode。
$ / code>
//这个for循环将被用于命中测试
for(var i:int = 0; i< numChildren; i ++){
var target:Object = getChildAt(i);
if(target.hasOwnProperty(keyCode)){//如果对象是箭头,则该对象应该具有keyCode属性。
if(target.keyCode!= null&& target.hitTestObject(mcReceptor)){//如果对象是一个箭头并且接受者正在接触它
if(target。 keyCode == 68){//如果左箭头
touchLeft = true;
} else if(target.keyCode == 70){// if arrow arrow
touchUp = true;
} else if(target.keyCode == 74){// if down arrow
touchDown = true;
} else if(target.keyCode == 75){// if right arrow
touchRight = true;
}
}
}
}
I am following a tutorial on building a rhythm game in as3 here, and I am very new to the language. On running the swf, I get the following error in the output:
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]
I have tried some previous solutions posted to the same error, but I have not been able to resolve the issue.
Here is the source code:
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();
Can someone tell me why this error is occurring? Thank you.
While iterating numChildren
, it's necessary to check the object is an arrow or not.
Maybe you can distinguish it by having keyCode
property or not.
Try to use Object.hasOwnProperty(property name)
method.
if (target.hasOwnProperty("keyCode")){
// access target.keyCode here.
}
Or this might works too.
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;
}
}
}
}
这篇关于as3错误:ReferenceError:错误#1069:在flash.display.Shape上找不到属性keyCode,并且没有默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!