Jeg kan simpelthen ikke finde ud af, hvorfor jeg bliver ved med af få denne her Error.
- I får bare hele koden, jeg håber ikke at det er for meget, men Debug fortæller mig (så sød, som den nu er) at fejlen sker ved flashDeploy (line: 376) og randomSpawnDeploy (line: 291)
___________________________________
import flash.display.MovieClip;
import flash.text.TextField;
import flash.display.Sprite;
import flash.events.TimerEvent;
import flash.events.Event;
// Set up variables
var isJumping:Boolean;
var landID:int;
var gameSpeed:int;
var obstacleOneClips:Array;
var obstacleTwoClips:Array;
var obstacleThreeClips:Array;
var obstacleFourClips:Array;
var deployableOneClips:Array;
var deployableTwoClips:Array;
var deployableThreeClips:Array;
var deployableFourClips:Array;
var deployableFiveClips:Array;
var deployableSixClips:Array;
var deployableSevenClips:Array;
var deployableEightClips:Array;
var deployableNineClips:Array;
var deployableTenClips:Array;
var obstacleSpawnID:int;
var obstacleFlashID:int;
var deploySpawnID:int;
var deployFlashID:int;
var deathFlashTimer:Timer;
var lives:int;
var score:int;
var carriedItems:int;
var collectPoints:int;
function init():void
{
isJumping = false;
gameSpeed = 500;
// Keyboard controls for stage
stage.addEventListener(KeyboardEvent.KEY_UP,onKeyReleased);
// this.addEventListener(Event.Deployable1_mc,checkLoaded);
// Set up Array
obstacleOneClips=new Array();
obstacleTwoClips=new Array();
obstacleThreeClips=new Array();
obstacleFourClips=new Array();
obstacleSpawnID = setInterval(randomSpawnObstacle,gameSpeed / 3);
obstacleFlashID = setInterval(flashObstacle,gameSpeed);
deployableOneClips:new Array();
deployableTwoClips:new Array();
deployableThreeClips:new Array();
deployableFourClips:new Array();
deployableFiveClips:new Array();
deployableSixClips:new Array();
deployableSevenClips:new Array();
deployableEightClips:new Array();
deployableNineClips:new Array();
deployableTenClips:new Array();
deploySpawnID = setInterval(randomSpawnDeploy,gameSpeed / 2);
deployFlashID = setInterval(flashDeploy,gameSpeed);
deathFlashTimer = new Timer(gameSpeed,6);
deathFlashTimer.addEventListener(TimerEvent.TIMER,flashChar);
deathFlashTimer.addEventListener(TimerEvent.TIMER_COMPLETE,charDeath);
lives = 1;
score = 99999;
updateScore(0);
carriedItems = 0;
}// Call function, store in e:
init();
function onKeyReleased(e:KeyboardEvent):void
{//trace("Keyboard passed")
switch (e.keyCode)
{// Keyboard movements
case Keyboard.UP :
if (char_mc.currentFrame != char_mc.upFrame)
{
if (checkCharObstacleHit("up"))
{
charDied();
}
else
{
char_mc.gotoAndStop(char_mc.upFrame);
}
}
break;
case Keyboard.DOWN :
if (char_mc.currentFrame != char_mc.downFrame)
{
if (checkCharObstacleHit("down"))
{
charDied();
}
else
{
char_mc.gotoAndStop(char_mc.downFrame);
}
}
break;
case Keyboard.LEFT :
if (char_mc.currentFrame != char_mc.leftFrame)
{
if (checkCharObstacleHit("left"))
{
charDied();
}
else
{
char_mc.gotoAndStop(char_mc.leftFrame);
}
}
break;
case Keyboard.RIGHT :
if (char_mc.currentFrame != char_mc.rightFrame)
{
if (checkCharObstacleHit("right"))
{
charDied();
}
else
{
char_mc.gotoAndStop(char_mc.rightFrame);
}
}
break;
case Keyboard.SPACE :
trace("Collect");
break;
}
}
function resetGame(e:TimerEvent=null):void
{
char_mc.gotoAndStop(1);
deathFlashTimer.reset();
var i:int;
var o:MovieClip;
for (i=obstacleOneClips.length-1; i>=0; i--)
{
o = obstacleOneClips[i];
removeChild(o);
obstacleOneClips.splice(i,1);
}
for (i=obstacleTwoClips.length-1; i>=0; i--)
{
o = obstacleTwoClips[i];
removeChild(o);
obstacleTwoClips.splice(i,1);
}
for (i=obstacleThreeClips.length-1; i>=0; i--)
{
o = obstacleThreeClips[i];
removeChild(o);
obstacleThreeClips.splice(i,1);
}
for (i=obstacleFourClips.length-1; i>=0; i--)
{
o = obstacleFourClips[i];
removeChild(o);
obstacleFourClips.splice(i,1);
}
resumeGame();
}
function pauseGame():void
{
stage.removeEventListener(KeyboardEvent.KEY_UP,onKeyReleased);
clearInterval(obstacleSpawnID);
clearInterval(obstacleFlashID);
clearInterval(deploySpawnID);
clearInterval(deploySpawnID);
}
function resumeGame():void
{
stage.addEventListener(KeyboardEvent.KEY_UP,onKeyReleased);
obstacleSpawnID = setInterval(randomSpawnObstacle,gameSpeed / 3);
obstacleFlashID = setInterval(flashObstacle,gameSpeed);
deploySpawnID = setInterval(randomSpawnDeploy,gameSpeed / 2);
deployFlashID = setInterval(flashDeploy,gameSpeed);
}
function charDied():void
{
pauseGame();
deathFlashTimer.start();
}
function flashChar(e:TimerEvent):void
{
char_mc.visible = ! char_mc.visible;
}
function charDeath(e:TimerEvent):void
{//trace('death');
if (lives>0)
{
resetGame();
}
}// Obstacle change which to set difficulty
function randomSpawnObstacle():void
{
var randomSpawnO:int = Math.ceil(Math.random() * 4);
if (randomSpawnO==1)
{// Change to set difficulty
var whichO:int = Math.ceil(Math.random() * 12);
var newObstacle:MovieClip;
if (whichO==1)
{
newObstacle = new Obstacle1_mc();
obstacleOneClips.push(newObstacle);
newObstacle.x = 539;
newObstacle.y = 288;
}
if (whichO==2)
{
newObstacle = new Obstacle2_mc();
obstacleTwoClips.push(newObstacle);
newObstacle.x = 708;
newObstacle.y = 231;
}
if (whichO==3)
{
newObstacle = new Obstacle3_mc();
obstacleThreeClips.push(newObstacle);
newObstacle.x = 504;
newObstacle.y = 138;
}
if (whichO==4)
{
newObstacle = new Obstacle4_mc();
obstacleFourClips.push(newObstacle);
newObstacle.x = 313;
newObstacle.y = 144;
}
if (newObstacle != null)
{
addChild(newObstacle);
}
}
}
function flashObstacle():void
{
var i:int;
var o:MovieClip;
for (i=obstacleOneClips.length-1; i>=0; i--)
{
o = obstacleOneClips[i];
o.gotoAndStop(o.currentFrame+1);
if (o.currentFrame == o.totalFrames)
{
removeChild(o);
obstacleOneClips.splice(i,1);
}
}
for (i=obstacleTwoClips.length-1; i>=0; i--)
{
o = obstacleTwoClips[i];
o.gotoAndStop(o.currentFrame+1);
if (o.currentFrame == o.totalFrames)
{
removeChild(o);
obstacleTwoClips.splice(i,1);
}
}
for (i=obstacleThreeClips.length-1; i>=0; i--)
{
o = obstacleThreeClips[i];
o.gotoAndStop(o.currentFrame+1);
if (o.currentFrame == o.totalFrames)
{
removeChild(o);
obstacleThreeClips.splice(i,1);
}
}
for (i=obstacleFourClips.length-1; i>=0; i--)
{
o = obstacleFourClips[i];
o.gotoAndStop(o.currentFrame+1);
if (o.currentFrame == o.totalFrames)
{
removeChild(o);
obstacleFourClips.splice(i,1);
}
}
}
// Deployable change which to set difficulty;
function randomSpawnDeploy():void
{
var randomSpawnD:int = Math.ceil(Math.random() * 10);
if (randomSpawnD==1)
{// Change to set difficulty
var whichD:int = Math.ceil(Math.random() * 30);
var newDeploy:MovieClip;
if (whichD==1)
{
newDeploy = new Deployable1_mc ();
deployableOneClips.push(newDeploy);
newDeploy.x = 638;
newDeploy.y = 227;
}
if (whichD==2)
{
newDeploy = new Deployable2_mc ();
deployableTwoClips.push(newDeploy);
newDeploy.x = 577;
newDeploy.y = 227;
}
if (whichD==3)
{
newDeploy = new Deployable3_mc ();
deployableThreeClips.push(newDeploy);
newDeploy.x = 338;
newDeploy.y = 140;
}
if (whichD==4)
{
newDeploy = new Deployable4_mc ();
deployableFourClips.push(newDeploy);
newDeploy.x = 557;
newDeploy.y = 140;
}
if (whichD==5)
{
newDeploy = new Deployable5_mc ();
deployableFiveClips.push(newDeploy);
newDeploy.x = 442;
newDeploy.y = 227;
}
if (whichD==6)
{
newDeploy = new Deployable6_mc ();
deployableSixClips.push(newDeploy);
newDeploy.x = 3774;
newDeploy.y = 227;
}
if (whichD==7)
{
newDeploy = new Deployable7_mc ();
deployableSevenClips.push(newDeploy);
newDeploy.x = 442;
newDeploy.y = 142;
}
if (whichD==8)
{
newDeploy = new Deployable8_mc () ;
deployableEightClips.push(newDeploy);
newDeploy.x = 374;
newDeploy.y = 142;
}
if (whichD==9)
{
newDeploy = new Deployable9_mc ();
deployableNineClips.push(newDeploy);
newDeploy.x = 760;
newDeploy.y = 190;
}
if (whichD==10)
{
newDeploy = new Deployable10_mc () ;
deployableTenClips.push(newDeploy);
newDeploy.x = 258;
newDeploy.y = 190;
}
if (newDeploy != null)
{
addChild(newDeploy);
}
}
}
function flashDeploy():void
{
var i:int;
var d:MovieClip;
for (i=deployableOneClips.length-1; i>=0; i--)
{
d = deployableOneClips[i];
d.gotoAndStop(d.currentFrame+1);
if (d.currentFrame == d.totalFrames)
{
removeChild(d);
deployableOneClips.splice(i,1);
}
}
for (i=deployableTwoClips.length-1; i>=0; i--)
{
d = deployableTwoClips[i];
d.gotoAndStop(d.currentFrame+1);
if (d.currentFrame == d.totalFrames)
{
removeChild(d);
deployableTwoClips.splice(i,1);
}
}
for (i=deployableThreeClips.length-1; i>=0; i--)
{
d = deployableThreeClips[i];
d.gotoAndStop(d.currentFrame+1);
if (d.currentFrame == d.totalFrames)
{
removeChild(d);
deployableThreeClips.splice(i,1);
}
}
for (i=deployableFourClips.length-1; i>=0; i--)
{
d = deployableFourClips[i];
d.gotoAndStop(d.currentFrame+1);
if (d.currentFrame == d.totalFrames)
{
removeChild(d);
deployableFourClips.splice(i,1);
}
}
for (i=deployableFiveClips.length-1; i>=0; i--)
{
d = deployableFiveClips[i];
d.gotoAndStop(d.currentFrame+1);
if (d.currentFrame == d.totalFrames)
{
removeChild(d);
deployableFiveClips.splice(i,1);
}
}
for (i=deployableSixClips.length-1; i>=0; i--)
{
d = deployableSixClips[i];
d.gotoAndStop(d.currentFrame+1);
if (d.currentFrame == d.totalFrames)
{
removeChild(d);
deployableSixClips.splice(i,1);
}
}
for (i=deployableSevenClips.length-1; i>=0; i--)
{
d = deployableSevenClips[i];
d.gotoAndStop(d.currentFrame+1);
if (d.currentFrame == d.totalFrames)
{
removeChild(d);
deployableSevenClips.splice(i,1);
}
}
for (i=deployableEightClips.length-1; i>=0; i--)
{
d = deployableEightClips[i];
d.gotoAndStop(d.currentFrame+1);
if (d.currentFrame == d.totalFrames)
{
removeChild(d);
deployableEightClips.splice(i,1);
}
}
for (i=deployableNineClips.length-1; i>=0; i--)
{
d = deployableNineClips[i];
d.gotoAndStop(d.currentFrame+1);
if (d.currentFrame == d.totalFrames)
{
removeChild(d);
deployableNineClips.splice(i,1);
}
}
for (i=deployableTenClips.length-1; i>=0; i--)
{
d = deployableTenClips[i];
d.gotoAndStop(d.currentFrame+1);
if (d.currentFrame == d.totalFrames)
{
removeChild(d);
deployableTenClips.splice(i,1);
}
}
}
function checkCharObstacleHit(dir:String):Boolean
{
var i:int;
var o:MovieClip;// Going Up (obstacle 1 has no up)
if (dir=="up")
{
for (i=obstacleTwoClips.length-1; i>=0; i--)
{
o = obstacleTwoClips[i];
switch (char_mc.currentFrame)
{
case 3 :
if (o.currentFrame >= 7)
{
return true;
}
break;
}
}
for (i=obstacleThreeClips.length-1; i>=0; i--)
{
o = obstacleThreeClips[i];
switch (char_mc.currentFrame)
{
case 19 :
if (o.currentFrame >= 7)
{
return true;
}
break;
}
}
for (i=obstacleFourClips.length-1; i>=0; i--)
{
o = obstacleFourClips[i];
switch (char_mc.currentFrame)
{
case 10 :
if (o.currentFrame >= 7)
{
return true;
}
break;
}
}
}// Going down (obstacle 1 has no down)
if (dir=="down")
{
for (i=obstacleTwoClips.length-1; i>=0; i--)
{
o = obstacleTwoClips[i];
switch (char_mc.currentFrame)
{
case 18 :
if (o.currentFrame >= 7)
{
return true;
}
break;
}
}
for (i=obstacleThreeClips.length-1; i>=0; i--)
{
o = obstacleThreeClips[i];
switch (char_mc.currentFrame)
{
case 14 :
if (o.currentFrame >= 7)
{
return true;
}
break;
}
}
for (i=obstacleFourClips.length-1; i>=0; i--)
{
o = obstacleFourClips[i];
switch (char_mc.currentFrame)
{
case 11 :
if (o.currentFrame >= 7)
{
return true;
}
break;
}
}
}// Only obstacle 1 has left and right
if (dir=="left")
{
for (i=obstacleOneClips.length-1; i>0; i--)
{
o = obstacleOneClips[i];
switch (char_mc.currentFrame)
{
case 5 :
if (o.currentFrame >= 7)
{
return true;
}
break;
}
}
}
if (dir=="right")
{
for (i=obstacleOneClips.length-1; i>0; i--)
{
o = obstacleOneClips[i];
switch (char_mc.currentFrame)
{
case 6 :
if (o.currentFrame >= 7)
{
return true;
}
break;
}
}
}
return false;
}
function updateScore(amount:int):void
{
score += amount;
Score_txt.text = "" + score;
}
6 kommentarer
Det er sandsynligvis fordi du et sted i i en af funktionerne prøver at instantiere et objekt som ikke eksisterer, og du derfor får en null error. Jeg orker ikke lige, at køre det igennem flash builder. Hvilken en af funktionerne kalder den anden funktion i sig? Du kan ignorere denne, og fokusere på den der kaldes "sidst".
Jeg vil foreslå, at du sætter nogle traces ind, og ser hvor langt det kommer, før de når til det sted hvor du har en null værdi der ikke var forventet.
Problemet er, at jeg får null værdi ved alle mine:
deployableOneClips
deployableTwoClips
deployableThreeClips
etc etc. og de er erklæret i toppen.
Det er vist måden du instantierer arrays på.
Prøv at ændre:
deployableOneClips:new Array();
til
deployableOneClips=new Array();
... Jeg mangler ord for min idioti! Og det brugte jeg 10 timer på
Hehe - ja, man kan godt stirre sig blind en gang imellem
//Pitter
Nvm fandt ud af det. Tak for hjælpen!