![](/img/trans.png)
[英]Why am I receiving a flash player security error when trying to access facebook graph api photos?
[英]Why am I receiving a null error when I do a `hitTest` on a defined object?
我要做的就是從舞台上刪除一些孩子,但是卻給了我一個空錯誤:
TypeError: Error #2007: Parameter hitTestObject must be non-null.
at flash.display::DisplayObject/flash.display:DisplayObject::_hitTest()
at flash.display::DisplayObject/hitTestObject()
at SpaceOddessey_fla::MainTimeline/GreenMove()
我不明白為什么會這樣,因為我在“ Green Guy”的移動/刪除功能上方的功能中定義了對象。 在修復此項目之前,我無法繼續進行該項目,因為我還需要將其用於其余的精靈。
這是我的代碼
stop();
//do this three times (for each space dude)
//GREEN
var MaxGreen:int = 1;
//Here we declare the speed of these cannon balls
var GreenVelocityX:Array = new Array();
var GreenVelocityY:Array = new Array();
//keep track of how many green guys
var GreenCount:int = 0;
var GreenSprite:Sprite;//declare a global variable for the cannonBall sprite.
var theGreen:Green_Guy;
//create the array thats holds the green guys
var myGreenGuyArray:Array = new Array();
//YELLOW
var MaxYellow:int = 1;
//Here we declare the speed of these cannon balls
var YellowVelocityX:Array = new Array();
var YellowVelocityY:Array = new Array();
//keep track of how many yellow guys
var YellowCount:int = 0;
var YellowSprite:Sprite;//declare a global variable for the cannonBall sprite.
var theYellow:Yellow_Guy;
//create the array thats holds the yellow guys
var myYellowGuyArray:Array = new Array();
//BABY
var MaxBabies:int = 1;
//Here we declare the speed of these cannon balls
var BabyVelocityX:Array = new Array();
var BabyVelocityY:Array = new Array();
//keep track of how many baby guys
var BabyCount:int = 0;
var BabySprite:Sprite;//declare a global variable for the cannonBall sprite.
var theBaby:Baby_Guy;
//create the array thats holds the baby guys
var myBabyGuyArray:Array = new Array();
//SPACE SHIP
var MaxShips:int = 4;
//Here we declare the speed of these cannon balls
var ShipVelocityX:Array = new Array();
var ShipVelocityY:Array = new Array();
//keep track of how many baby guys
var ShipCount:int = 0;
var ShipSprite:Sprite;//declare a global variable for the cannonBall sprite.
var theShip:space_ship;
//create the array thats holds the baby guys
var ShipArray:Array = new Array();
//ASTEROID
var MaxRocks:int = 6;
//Here we declare the speed of these cannon balls
var RockVelocityX:Array = new Array();
var RockVelocityY:Array = new Array();
//keep track of how many baby guys
var RockCount:int = 0;
var RockSprite:Sprite;//declare a global variable for the cannonBall sprite.
var theRock:rock;
//create the array thats holds the baby guys
var RockArray:Array = new Array();
yellow_btn.addEventListener(MouseEvent.CLICK, ClickHandle);
green_btn.addEventListener(MouseEvent.CLICK, ClickHandle);
baby_btn.addEventListener(MouseEvent.CLICK, ClickHandle);
function ClickHandle(targetEvent:MouseEvent):void
{
if(targetEvent.target == yellow_btn)
{
MaxYellow += 1;
stage.addEventListener(Event.ENTER_FRAME,makeYellow);
}
else if(targetEvent.target == green_btn)
{
MaxGreen += 1;
stage.addEventListener(Event.ENTER_FRAME,makeGreen);
}
else if(targetEvent.target == baby_btn)
{
MaxBabies += 1;
stage.addEventListener(Event.ENTER_FRAME,makeBaby);
}
}
stage.addEventListener(Event.ENTER_FRAME,makeShips);
function makeShips(Space:Event){
//Create the Enemies if there are less then 100
if (ShipCount < MaxShips){
//create our sprites
ShipSprite = new Sprite();
stage.addChild(ShipSprite);
theShip = new space_ship();
ShipSprite.addChild(theShip);
ShipSprite.x = Math.random() * 500;//width ~ 81.7
ShipSprite.y = Math.random() * 300;
//allows a scaler of making objects from big to small
//BabySprite.scaleX = BabySprite.scaleY = Math.random() * 1.3;
ShipArray[ShipCount] = ShipSprite;
ShipCount += 1;
stage.addEventListener(Event.ENTER_FRAME,ShipMove);
}
//end for loop
}
function ShipMove(go:Event):void
{
for(var s=0; s<ShipCount; s++)
{
ShipVelocityX[s] = -3;
ShipVelocityY[s] = -1;
/*if(my)
{
}*/
ShipArray[s].x += ShipVelocityX[s];
//myYellowGuyArray[a].y += YellowVelocityY[a];
if (ShipArray[s].x > 550)
{
ShipArray[s].x = 0 - ShipArray[s].width;
}
else if(ShipArray[s].x + ShipArray[s].width < 0)
{
ShipArray[s].x = 500;
}
if(ShipArray[s].y > 400)
{
ShipArray[s].y = 0 - ShipArray[s].height;
}
else if(ShipArray[s].y + ShipArray[s].height < 0)
{
ShipArray[s].y = 400;
}
}
}
stage.addEventListener(Event.ENTER_FRAME,makeBaby);
function makeBaby(DudeEvent:Event){
//Create the Enemies if there are less then 100
if (BabyCount < MaxBabies){
//create our sprites
BabySprite = new Sprite();
stage.addChild(BabySprite);
theBaby = new Baby_Guy();
BabySprite.addChild(theBaby);
BabySprite.x = Math.random() * 500;//width ~ 81.7
BabySprite.y = Math.random() * 300;
//allows a scaler of making objects from big to small
//BabySprite.scaleX = BabySprite.scaleY = Math.random() * 1.3;
myBabyGuyArray[BabyCount] = BabySprite;
BabyCount += 1;
//trace(myEnemies.length);
stage.addEventListener(Event.ENTER_FRAME,BabyMove);
}
//end for loop
}
function BabyMove(go:Event):void
{
for(var i=0; i<BabyCount; i++)
{
BabyVelocityX[i] = 5;
BabyVelocityY[i] = 2;
/*if(my)
{
}*/
myBabyGuyArray[i].x += BabyVelocityX[i];
myBabyGuyArray[i].y += BabyVelocityY[i];
myBabyGuyArray[i].rotation += 2;
if (myBabyGuyArray[i].x > 550)
{
myBabyGuyArray[i].x = 0 - myBabyGuyArray[i].width;
}
else if(myBabyGuyArray[i].x + myBabyGuyArray[i].width < 0)
{
myBabyGuyArray[i].x = 500;
}
if(myBabyGuyArray[i].y > 400)
{
myBabyGuyArray[i].y = 0 - myBabyGuyArray[i].height;
}
else if(myBabyGuyArray[i].y + myBabyGuyArray[i].height < 0)
{
myBabyGuyArray[i].y = 400;
}
for(var v:int=0; v<RockCount; v++)
{
if(RockArray[v].hitTestObject(myBabyGuyArray[i]))
{
//trace('hey')
BabyVelocityX[i] = 100;
}
}
}
}
stage.addEventListener(Event.ENTER_FRAME,makeYellow);
function makeYellow(Dude:Event){
//Create the Enemies if there are less then 100
if (YellowCount < MaxYellow){
//create our sprites
YellowSprite = new Sprite();
stage.addChild(YellowSprite);
theYellow = new Yellow_Guy();
YellowSprite.addChild(theYellow);
YellowSprite.x = Math.random() * 500;//width ~ 81.7
YellowSprite.y = Math.random() * 300;
//allows a scaler of making objects from big to small
//BabySprite.scaleX = BabySprite.scaleY = Math.random() * 1.3;
myYellowGuyArray[YellowCount] = YellowSprite;
YellowCount += 1;
stage.addEventListener(Event.ENTER_FRAME,YellowMove);
}
//end for loop
}
function YellowMove(go:Event):void
{
for(var a=0; a<YellowCount; a++)
{
YellowVelocityX[a] = -3;
YellowVelocityY[a] = -1;
/*if(my)
{
}*/
myYellowGuyArray[a].x += YellowVelocityX[a];
myYellowGuyArray[a].y += YellowVelocityY[a];
myYellowGuyArray[a].rotation += 1;
if (myYellowGuyArray[a].x > 550)
{
myYellowGuyArray[a].x = 0 - myYellowGuyArray[a].width;
}
else if(myYellowGuyArray[a].x + myYellowGuyArray[a].width < 0)
{
myYellowGuyArray[a].x = 500;
}
if(myYellowGuyArray[a].y > 400)
{
myYellowGuyArray[a].y = 0 - myYellowGuyArray[a].height;
}
else if(myYellowGuyArray[a].y + myYellowGuyArray[a].height < 0)
{
myYellowGuyArray[a].y = 400;
}
for(var u:int=0; u<RockCount; u++)
{
if(RockArray[u].hitTestObject(myYellowGuyArray[a]))
{
YellowVelocityX[a] *= -1;
}
}
}
}
stage.addEventListener(Event.ENTER_FRAME,makeGreen);
function makeGreen(Evnt:Event){
//Create the Enemies if there are less then 100
if (GreenCount < MaxGreen){
//create our sprites
GreenSprite = new Sprite();
stage.addChild(GreenSprite);
theGreen = new Green_Guy();
GreenSprite.addChild(theGreen);
GreenSprite.x = Math.random() * 500;//width ~ 81.7
GreenSprite.y = Math.random() * 300;
//allows a scaler of making objects from big to small
//BabySprite.scaleX = BabySprite.scaleY = Math.random() * 1.3;
myGreenGuyArray[GreenCount] = GreenSprite;
GreenCount += 1;
stage.addEventListener(Event.ENTER_FRAME,GreenMove);
}
//end for loop
}
function GreenMove(go:Event):void
{
for(var b:int=0; b<GreenCount; b++)
{
GreenVelocityX[b] = 7;
GreenVelocityY[b] = -4;
myGreenGuyArray[b].x += GreenVelocityX[b];
myGreenGuyArray[b].y += GreenVelocityY[b];
myGreenGuyArray[b].rotation -= 1;
if (myGreenGuyArray[b].x > 550)
{
myGreenGuyArray[b].x = 0 - myGreenGuyArray[b].width;
}
else if(myGreenGuyArray[b].x + myGreenGuyArray[b].width < 0)
{
myGreenGuyArray[b].x = 500;
}
if(myGreenGuyArray[b].y > 400)
{
myGreenGuyArray[b].y = 0 - myGreenGuyArray[b].height;
}
else if(myGreenGuyArray[b].y + myGreenGuyArray[b].height < 0)
{
myGreenGuyArray[b].y = 400;
}
for(var z:int=0; z<ShipCount; z++)
{
//if(myGreenGuyArray[b] == null)
{
if(ShipArray[z].hitTestObject(myGreenGuyArray[b]))
{
//if(GreenCount > 1)
//{
myGreenGuyArray[b].y = -50;
stage.removeChild(myGreenGuyArray[b]);
myGreenGuyArray.splice(b,1);
GreenCount--;
//b--;
//}
}
}
}
for(var g:int=0; g<RockCount; g++)
{
if(RockArray[g].hitTestObject(myGreenGuyArray[b]))
{
GreenVelocityX[b] *= -1;
}
}
}
}
stage.addEventListener(Event.ENTER_FRAME,makeRocks);
function makeRocks(RockHard:Event){
//Create the Enemies if there are less then 100
if (RockCount < MaxRocks){
//create our sprites
RockSprite = new Sprite();
stage.addChild(RockSprite);
theRock = new rock();
RockSprite.addChild(theRock);
RockSprite.x = Math.random() * 500;//width ~ 81.7
RockSprite.y = Math.random() * 300;
//allows a scaler of making objects from big to small
//BabySprite.scaleX = BabySprite.scaleY = Math.random() * 1.3;
RockArray[RockCount] = RockSprite;
RockCount += 1;
stage.addEventListener(Event.ENTER_FRAME,RockMove);
}
//end for loop
}
function RockMove(go:Event):void
{
for(var r=0; r<RockCount; r++)
{
RockVelocityX[r] = 7;
RockVelocityY[r] = 3;
/*if(my)
{
}*/
RockArray[r].x += RockVelocityX[r];
//RockArray[r].y += RockVelocityY[r];
if (RockArray[r].x > 550)
{
RockArray[r].x = 0 - RockArray[r].width;
}
else if(RockArray[r].x + RockArray[r].width < 0)
{
RockArray[r].x = 500;
}
if(RockArray[r].y > 400)
{
RockArray[r].y = 0 - RockArray[r].height;
}
else if(RockArray[r].y + RockArray[r].height < 0)
{
RockArray[r].y = 400;
}
}
}
您必須重組和簡化代碼。 您必須不惜一切代價消除嵌套的enter_frame事件。 理想情況下,您可以通過一個ENTER_FRAME事件來完成所有事情。
請按照與您的代碼最接近但更干凈的方式執行以下操作
但是,您可以進一步使它更清潔,但是至少要做到上述幾點,才能使您早日實現目標。
我不明白為什么會這樣
這聽起來微不足道,但這是由於錯誤描述中給出的原因而發生的:
參數hitTestObject必須為非null。
這是人們必須接受的真理。 沒有其他辦法了。 沒有“但我確定這不會發生”這樣的事情。 提出更好的論點並不能贏得討論。 從某種意義上說,編譯器總是正確的。 您不能反駁。
我在“ Green Guy”的移動/刪除功能上方的功能中定義了對象
不。該錯誤表明您沒有,所以您沒有。 該參數為null
。 否認事實是沒有意義的。
真正的問題不是“ 即使不是, 為什么也說它為null? ”,而是“ 為什么值為null? ”。
該錯誤表明它發生在hitTestObject()
函數中。 堆棧跟蹤說明它發生在GreenMove()
函數中。 有兩行可能會導致這種情況。
if(ShipArray[z].hitTestObject(myGreenGuyArray[b]))
和
if(RockArray[g].hitTestObject(myGreenGuyArray[b]))
您應該在每行放置一個斷點,再次使用調試器運行代碼,然后嘗試重現該錯誤。 這是調試代碼的方式。 您已經五個小時沒有看到它了,希望它會因您找不到原因而跳到您身上並感到沮喪。
發生錯誤時, hitTestObject
會認為myGreenGuyArray[b]
為null
。 由此,您知道b
值是什么。 再次運行。 使它再次失敗。 查看它是否總是以相同的b
值發生。
這行看起來很可疑,因為它改變了數組。
myGreenGuyArray.splice(b,1);
您的問題是由數組的混亂使用引起的。 想一想:如果只有一個元素被刪除怎么辦? 如果再次訪問該數組的(已刪除,不存在)元素,會發生什么情況?
trace([1,2,3][37] == null); // does the result of accessing an element that doesn't exist equal null?
為了解決這個問題
continue
和/或break
跳出嵌套循環。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.