1084: Syntax error: expecting rightbrace before end of program - actionscript-3

So I keep getting this error, debugger wont run but rather just shows this error in a compiler box.
Thing has been driving me crazy for days now so I decided to post it here.
So it says:
Scene 1, Layer 'powerups', Frame 1 1084: Syntax error: expecting rightbrace before end of program.
I designed my code so that the "powerups" layer contains only "powerup" functions,
heres the code from "Powerups" layer:
//this code holds the powerups
function extraCoins():void
{
coins = coins + 1000;
}
function doubleCoins():void
{
doubCoins = true;
}
function hyperBoostD():void
{
Boost = 600;
playerSpeed = playerSpeed + 150;
player.y = player.y + 300;
colBoolean = false;
hyperBoost.push(Boost);
}
function BoostD():void
{
Boost = 200;
playerSpeed = playerSpeed + 100;
player.y = player.y + 200;
colBoolean = false;
boost.push(Boost);
}
function multiplierDone():void
{
multiplier++;
}
function multiplierDtwo():void
{
multiplier = multiplier + 2;
}
function watchOutD():void
{
watchOut = true;
watchOutT = 600;
}
function blowOutD():void
{
blowOut = true;
blowOutT = 100;
}
function planeD():void
{
planeT = 600;
colBoolean = false;
}
function havenD():void
{
havenT = 200;
coinMake = 20;
}
function badBirdD():void
{
birdT = 600;
birdSet = 10;
}
function tricksterD():void
{
tricksterT = 600;
trickster = true;
}
function rampageD():void
{
rampageT = 600;
rampage = true;
pplStat = true;
var tempWatchoutPPL:MovieClip;
tempWatchoutPPL = new peopleWatchout();
tempWatchoutPPL.y = stage.stageHeight /2;
tempWatchoutPPL.x = stage.stageWidth /2;
addChild(tempWatchoutPPL);
signs.push(tempWatchoutPPL);
}
function helpPPLD():void
{
helpPPLT= 600;
helpPPL = true;
var tempHelpPPL:MovieClip;
tempHelpPPL = new savePpl();
tempHelpPPL.y = stage.stageHeight /2;
tempHelpPPL.x = stage.stageWidth /2;
addChild(tempHelpPPL);
signs.push(tempHelpPPL);
}
And here is my main code :
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.text.engine.SpaceJustifier;
import flashx.textLayout.operations.MoveChildrenOperation;
import flash.utils.Timer;
import flashx.textLayout.accessibility.TextAccImpl;
/*
CODE BY:
START DATE: 12.06.2013.
FINISH DATE: TBA
NOTE: My secound video game ever.
BUGs:
-obsticle collison stops working after some time playing
-passing coin gives you more than 5 coins
*/
/***..........................VARs.........................................................................***/
var STATE_START:String="STATE_START";
var STATE_START_PLAYER:String="STATE_START_PLAYER";
var STATE_PLAY:String="STATE_PLAY";
var STATE_END:String="STATE_END";
var gameState:String;
//Player
var player:MovieClip;
var playerSpeed:Number;
var speedLimit:Number;
var speedLimitInc:Number;
//score
var score:Number= 1;
var scoreInc:Number= 1;
//coins
var coins:Number= 1;
var balance:Number;
var coinCount:Number= 1;
var Coins:Array;
var doubCoins:Boolean = false;
var multiplier:int = 1;
var coinMake:Number = 60;
//distance
var meters:Number= 1;
var distance:Number= 1;
//holds drops
var drops:Array;
//terrain
//decides spawning time
var GenS:Number=1;
//holds terrain
var terrain:Array;
//boost
//holds the speedBoosts that are happening
var boost:Array;
//holds boost dysp obj.
var booost:Array;
//holds speedBoost duration
var Boost:Number;
//collision on/off
var colBoolean:Boolean=true;
var hyperBoost:Array;
//obsticles
var obsticles:Array;
//other
var level:Number= 1;
var levelCount:Number= 1;
var birdArray:Array;
var jumpState:Boolean = false;
var jumpStateT:int;
var jumps:Array;
//powerUps
var watchOut:Boolean = false;
var watchOutT:int;
var blowOut:Boolean = false;
var blowOutT:int;
var planeT:int;
var birdT:int;
var birdSet:int = 300;
var havenT:int;
var trickster:Boolean = false;
var tricksterT:int;
var ppl:Array;
//pplStat tells if ppl are getting hurt or need help, if ture, it means you are gonna run them over
var pplStat:Boolean = true;
var rampage:Boolean;
var rampageT:int;
var helpPPLT:int;
var helpPPL:Boolean;
var signs:Array;
/***.......................Display SETUP...................................................................***/
homeScreen.visible = true;
gamePlay.visible = false;
endScreen.visible = false;
/***........................Start Screen...................................................................***/
homeScreen.play_BTN.addEventListener(MouseEvent.CLICK, clickPlay);
homeScreen.settings_BTN.addEventListener(MouseEvent.CLICK, clickSettings);
function clickPlay(event:MouseEvent):void
{
//Move main screen from stage
homeScreen.visible = false;
//Begin loading the game
gameState = STATE_START;
trace (gameState);
addEventListener(Event.ENTER_FRAME, gameLoop);
}
function clickSettings(event:MouseEvent):void
{
//Move main screen from stage
homeScreen.visible = false;
//settingsScreen.visible...
}
/***...........................Game LOOP...................................................................***/
function gameLoop(e:Event):void
{
switch(gameState)
{
case STATE_START:
startGame();
break;
case STATE_START_PLAYER:
startPlayer();
break;
case STATE_PLAY:
playGame();
break;
case STATE_END:
endGame();
break;
}
}
/***...............STATE_START.............................................................................***/
function startGame():void
{
level = 1;
playerSpeed = 1;
speedLimit = 10;
speedLimitInc = 1;
//Graphics
//coins
Coins = new Array();
//player icon
player = new Player();
//start obsticles array
obsticles = new Array();
//holds terrain (speedUps, jupms, etc)
terrain = new Array();
//holds speedUps
boost = new Array();
booost = new Array();
hyperBoost = new Array();
//other
drops = new Array();
birdArray = new Array();
jumps = new Array();
ppl = new Array();
signs = new Array();
gameState = STATE_START_PLAYER;
trace(gameState);
}
/***....................STATE_START_PLAYER................................................................***/
function startPlayer():void
{
//start the player
//set possition of player
player.y = player.height + 10;
addChild(player);
addEventListener(Event.ENTER_FRAME, movePlayer);
//changing screens
//start game
gameState = STATE_PLAY;
trace(gameState);
}
//player controll
function movePlayer(e:Event):void
{
//mouse\touch recognition
player.x = stage.mouseX;
//making sure player does not move out of the stage
if (player.x < 0)
{
player.x = 0;
}
if (player.x > (stage.stageWidth - 2/player.width))
{
player.x = stage.stageWidth + 2/player.width;
}
}
/***............................STATE_PLAY................................................................***/
function playGame():void
{
if (watchOutT <= 0) {watchOut = false}
if (blowOutT <= 0) {blowOut = false}
if (planeT <= 0) {colBoolean = true}
if (tricksterT <= 0) {trickster= false}
if (havenT <= 0) {coinMake = 60}
if (birdT <= 0) {birdSet = 300}
if (planeT >= 0) {planeT--} birdT
if (havenT >= 0) {havenT--}
if (birdT >= 0) {birdT--}
if (tricksterT >= 0) {tricksterT--}
speedUp();
metersCount();
scoreCount();
makeCoins();
moveCoins();
makeTerrain();
moveDrop();
makeBird();
moveBird();
moveJump();
moveSigns();
movePPL();
stunts();
//speed boost
moveSpeedBoost();
//obsticles
moveObsticle();
}
function metersCount():void
{
meters = meters + playerSpeed;
distance = meters / 50;
if (distance >= 1000 && distance <= 1200)
{
levelCount= 1000;
level = 2;
}
if (distance >= (levelCount + 1000) && distance <= (levelCount + 1200))
{
level++;
levelCount= levelCount + 1000;
}
trace("level", level);
trace("meters", meters);
trace("coins", coins);
trace("distance", distance);
}
function scoreCount():void
{
if (scoreInc >= 30) { score++; scoreInc=1; trace("-score", score); }
scoreInc++
}
function makeCoins():void
{
trace("coinCount", coinCount);
if (coinCount == coinMake)
{
var tempCoin:MovieClip;
//generate enemies
tempCoin = new Coin();
tempCoin.speed = playerSpeed;
tempCoin.x = Math.round(Math.random()*400);
tempCoin.y = stage.stageHeight;
addChild(tempCoin);
Coins.push(tempCoin);
coinCount = 1;
}
coinCount++;
}
function moveCoins():void
{
var j:int;
var tempCoin:MovieClip;
for(var i:int = Coins.length-1; i>=0; i--)
{
tempCoin = Coins[i];
tempCoin.y = tempCoin.y - playerSpeed;
}
//testion colision with the player and screen out
if (tempCoin != null)
{
if (tempCoin.y > stage.stageHeight)
{
removeCoin(i);
}
if (tempCoin.hitTestObject(player))
{
if (i != j && doubCoins == false) {coins = coins + 5; j = i;}
if (i != j && doubCoins == true) {coins = coins + 10; j = i;}
removeCoin(i);
}
}
}
function speedUp():void
{
trace ("speed", playerSpeed);
//checks if any boosts are on
var k:Boolean;
//making speed limit
if (playerSpeed < speedLimit)
{
playerSpeed ++;
if (k == true) {j++}
}
//increasing speed limit
if (speedLimitInc == 100)
{
speedLimit ++;
speedLimitInc = 1;
}
speedLimitInc ++;
var j:Number;
j = playerSpeed;
for (var i:int = boost.length-1; i>=0; i--)
{
if (tempBoostN >= 0)
{k = true; colBoolean = true;}
else
{
k = false;
player.y = player.height + 30;
}
var tempBoostN = boost[i];
if (playerSpeed >= j)
{
if (tempBoostN >= 150)
{
playerSpeed = playerSpeed -1;
} else if (tempBoostN <= 150 && tempBoostN >= 30) {
playerSpeed = playerSpeed - 2;
}
tempBoostN--;
}
}
var l:Number;
l = playerSpeed;
for (var i:int = boost.length-1; i>=0; i--)
{
if (tempBoostN >= 0)
{k = true; colBoolean = true;}
else
{
k = false;
player.y = player.height + 30;
}
var tempBoostH = hyperBoost[i];
if (playerSpeed >= l)
{
if (tempBoostH >= 150)
{
playerSpeed = playerSpeed -1;
} else if (tempBoostN <= 150 && tempBoostH >= 30) {
playerSpeed = playerSpeed - 2;
}
tempBoostH--;
}
}
}
function makeBos():void
{
var tempBoost:MovieClip;
tempBoost = new speedPod();
if (tempBoost != null)
{
tempBoost.y = stage.stageHeight;
tempBoost.x = Math.round(Math.random()*400);
booost.push(tempBoost);
var i = getChildIndex(player);
addChild(tempBoost);
setChildIndex (tempBoost, i);
}
}
function moveSpeedBoost():void
{
var tempBoost:MovieClip;
for(var i:int = booost.length-1; i>=0; i--)
{
tempBoost = booost[i];
tempBoost.y = tempBoost.y - playerSpeed;
}
//test if Boost is off-stage and set it to remove
if (tempBoost != null && tempBoost.y < stage.stageHeight)
{
removeSpeedBoost(i);
}
//player-Boost colision
if (tempBoost != null && tempBoost.hitTestObject(player))
{
Boost = 200;
playerSpeed = playerSpeed + 100;
player.y = player.y + 200;
colBoolean = false;
boost.push(Boost);
}
}
function makeTerrain():void
{
if (GenS == 29)
{
GenS = 1;
var genType:Number = Math.floor(Math.random()*100);
//POWERUPS
//handling watchout powerup
if (watchOut = true){watchOutT--; makeObs();}
//handling blowout powerup
if (blowOut = true){blowOutT--;}
//handling trickster powerup
if (trickster = true){makeJump();}
//handling rampage powerup
if (rampage = true){makePPL(0); rampageT--;}
//handling hurtPPL powerup
if (hurtPPL = true){makePPL(1); hurtPPLT--;}
//general spawning
if (genType >= 1 && genType <=20 && watchOut == false && blowOut == false && trickster == false)
{
trace("makeObs");
makeObs();
}else if (genType >= 20 && genType <=60 && watchOut == false && blowOut == false && trickster == false)
{
trace("makeBos");
makeBos();
}else if (genType >= 60 && genType <=65 && watchOut == false && blowOut == false && trickster == false)
{
trace("makeDrop");
makeDrop();
}else if (genType >= 65 && genType <=100 && watchOut == false && blowOut == false && trickster == false)
{
trace("make jump");
makeJump();
}
}
GenS++;
}
function makeObs():void
{
var tempObs:MovieClip;
//determining the type of an obsticle
var typeObs:Number = Math.floor(Math.random()*3);
switch (typeObs)
{
case 0:
tempObs = new wLog();
break;
case 1:
tempObs = new Spill();
break;
case 2:
tempObs = new wTree();
break;
}
if (tempObs != null)
{
tempObs.y = stage.stageHeight;
tempObs.x = Math.round(Math.random()*400);
addChild(tempObs);
obsticles.push(tempObs);
}
}
function makePPL():void
{
var tempPPL:MovieClip;
if (hurtPPL = true)
{tempPPL = new personHurt();}
else {tempPPL = new person();}
tempPPL.y = stage.stageHeight;
tempPPL.x = Math.round(Math.random()*400);
addChild(tempPPL);
ppl.push(tempPPL);
}
function movePPL():void
{
//move enemies
var tempPPL:MovieClip;
for(var i:int = ppl.length-1; i>=0; i--)
{
tempPPL = ppl[i];
tempPPL.y = tempPPL.y - playerSpeed;
}
//test if obsticle is off-stage and set it to remove
if (tempPPL != null && tempPPL.y < stage.stageHeight)
{
removePPL(i);
}
//player-obsticle colision
if (tempPPL != null && tempPPL.hitTestObject(player) && hurtPPL = true)
{
score = score + 1000;
coins = coins + 1000;
var tempSaved:MovieClip;
tempSaved = new savedPerson();
tempSaved.y = stage.stageHeight /2;
tempSaved.x = stage.stageWidth /2;
addChild(tempSaved);
signs.push(tempSaved);
trace ("person saved");
removePPL(i);
} else if (tempPPL != null && tempPPL.hitTestObject(player))
{
score = score - 1000;
var tempRanOver:MovieClip;
tempRanOver = new ranOver();
tempRanOver.y = stage.stageHeight /2;
tempRanOver.x = stage.stageWidth /2;
addChild(tempRanOver);
signs.push(tempRanOver);
trace ("ran over a person");
removePPL(i);
}
}
function moveSigns():void
{
//move enemies
var tempSign:MovieClip;
for(var i:int = signs.length-1; i>=0; i--)
{
tempSign = signs[i];
tempSign.y = tempSign.y - playerSpeed;
}
//test if obsticle is off-stage and set it to remove
if (tempSign != null && tempSign.y < stage.stageHeight)
{
removeSign(i);
}
}
function makeDrop():void
{
var tempDrop:MovieClip;
tempDrop = new Drop();
tempDrop.y = stage.stageHeight;
tempDrop.x = Math.round(Math.random()*400);
addChild(tempDrop);
drops.push(tempDrop);
}
function moveDrop():void
{
//move enemies
var tempDrop:MovieClip;
for(var i:int = drops.length-1; i>=0; i--)
{
tempDrop = drops[i];
tempDrop.y = tempDrop.y - playerSpeed;
}
//test if obsticle is off-stage and set it to remove
if (tempDrop != null && tempDrop.y < stage.stageHeight)
{
removeDrop(i);
}
//player-obsticle colision
if (tempDrop != null && tempDrop.hitTestObject(player))
{
powerUp();
}
}
function makeBird():void
{
if (distance >= 200)
{
var chance:Number = Math.floor(Math.random()*birdSet);
if (chance <= 1 + level)
{
var tempBird:MovieClip;
//generate enemies
tempBird = new BirdO();
tempBird.speed = 60 + playerSpeed;
tempBird.x = Math.round(Math.random()*400);
addChild(tempBird);
birdArray.push(tempBird);
}
}
}
function moveBird():void
{
var tempBird:MovieClip;
for(var i:int = birdArray.length-1; i>=0; i--)
{
tempBird = birdArray[i];
tempBird.y -= tempBird.speed;
}
if (tempBird != null)
{
if (tempBird.y > stage.stageHeight)
{
removeBird(i);
}
if (tempBird.hitTestObject(player))
{
gameState = STATE_END;
}
}
}
function powerUp():void
{
var dropType:Number = Math.floor(Math.random()*16);
switch (dropType)
{
case 0:
trace("extra coins");
extraCoins();
break;
case 4:
trace("2x coins");
doubleCoins();
break;
case 2:
trace("hyper boost");
hyperBoostD();
break;
case 3:
trace("boost");
BoostD();
break;
case 4:
trace("multiplier 1x");
multiplierDone();
break;
case 5:
trace("multiplier 2x");
multiplierDtwo();
break;
case 6:
trace("watch out!");
watchOutD();
break;
case 7:
trace("blowout");
blowOutD();
break;
case 8:
trace("plane");
planeD();
break;
case 9:
trace("haven");
havenD();
break;
case 10:
trace("bad bird");
badBirdD();
break;
case 11:
trace("trickster");
tricksterD();
break;
case 12:
trace("rampage");
rampageD();
break;
case 13:
break;
case 14:
break;
case 15:
break;
case 16:
break;
}
}
function makeJump():void
{
var tempJump:MovieClip;
tempJump = new Jump();
tempJump.speed = playerSpeed;
tempJump.x = Math.round(Math.random()*400);
addChild(tempJump);
jumps.push(tempJump);
}
function moveJump():void
{
var tempJump:MovieClip;
for(var i:int = jumps.length-1; i>=0; i--)
{
tempJump = jumps[i];
tempJump.y = tempJump.y - playerSpeed;
}
//test if obsticle is off-stage and set it to remove
if (tempJump != null && tempJump.y < stage.stageHeight)
{
removeJump(i);
}
//player-obsticle colision
if (tempJump != null && tempJump.hitTestObject(player))
{
jumpState = true;
jumpStateT = 100;
}
}
function stunts():void
{
if (jumpStateT >= 0)
{
jumpStateT--;
jumpState = true;
}
if (jumpState = true)
{
Multitouch.inputMode = MultitouchInputMode.GESTURE;
stage.addEventListener (TransformGestureEvent.GESTURE_SWIPE, fl_SwipeHandler);
}
}
function fl_SwipeHandler(event:TransformGestureEvent):void
{
switch(event.offsetX)
{
// swiped right
case 1:
{
// My code
trace ("side flip");
score = score + 1000;
coins = coins + 500;
break;
}
// swiped left
case -1:
{
// My code
trace ("barell roll");
score = score + 1000;
coins = coins + 500;
break;
}
}
switch(event.offsetY)
{
// swiped down
case 1:
{
trace ("front flip");
score = score + 1000;
coins = coins + 500;
break;
}
// swiped up
case -1:
{
trace ("side flip");
score = score + 1000;
coins = coins + 500;
break;
}
}
}
function moveObsticle():void
{
//move enemies
var tempObs:MovieClip;
for(var i:int = obsticles.length-1; i>=0; i--)
{
tempObs = obsticles[i];
tempObs.y = tempObs.y - playerSpeed;
}
//test if obsticle is off-stage and set it to remove
if (tempObs != null && tempObs.y < stage.stageHeight)
{
removeObsticle(i);
}
//player-obsticle colision
if (tempObs != null && tempObs.hitTestObject(player))
{
gameState = STATE_END;
}
}
/*REMOVING BS FROM STAGE*/
//remove obsticle
function removeObsticle(idx:int):void
{
if(idx >= 0)
{
removeChild(obsticles[idx]);
obsticles.splice(idx, 1);
}
}
function removeTer(idx:int):void
{
if(idx >= 0)
{
removeChild(terrain[idx]);
terrain.splice(idx, 1);
}
}
function removeSpeedBoost(idx:int):void
{
if(idx >= 0)
{
removeChild(boost[idx]);
boost.splice(idx, 1);
}
}
function removeCoin(idx:int):void
{
if(idx >= 0)
{
removeChild(Coins[idx]);
Coins.splice(idx, 1);
}
}
function removeDrop(idx:int):void
{
if(idx >= 0)
{
removeChild(drops[idx]);
drops.splice(idx, 1);
}
}
function removeBird(idx:int):void
{
if(idx >= 0)
{
removeChild(birdArray[idx]);
birdArray.splice(idx, 1);
}
function removeJump(idx:int):void
{
if(idx >= 0)
{
removeChild(jumps[idx]);
jumps.splice(idx, 1);
}
}
function removePPL(idx:int):void
{
if(idx >= 0)
{
removeChild(ppl[idx]);
ppl.splice(idx, 1);
}
}
function removeSigns(idx:int):void
{
if(idx >= 0)
{
removeChild(signs[idx]);
signs.splice(idx, 1);
}
}
/***.........................STATE_END....................................................................***/
function endGame():void
{
gamePlay.visible = false;
endScreen.visible = true;
}
I have checked times and times again for any syntax missing and have failed to find a problem.
I have seen a guy asking a question here about similar error and it turns out its not eaven a syntax error, he still didnt resolve the problem.
I have also indented all the code properly and eaven used "auto format" in flash pro cs6 to see if it screams any syntax errors and it didnt.
Im making a mobile game so all the code is running in the following environment:
AIR 3,2 for android
And of course as 3.0
Thanks in advance.

You are missing a close bracket } after this function: Try to read your code carefully before asking. Pretty easy to notice :)
function removeBird(idx:int):void
{
if(idx >= 0)
{
removeChild(birdArray[idx]);
birdArray.splice(idx, 1);
}

Related

Character keeps falling when it shouldn't be

I am doing an assessment task for school in as3 and I have made a feature that will let the character fall if it is not standing on top of something but the boolean variable falling keeps being on when it is not supposed to. I have figured out that what causes it to stop for a short time and then fall again past what it is standing on is that the velocity valuable increases past the floor it is on so I reset the velocity value in the code in another spot but that does not solve the issue of the falling boolean being true when it is not supposed too.
I have put a comment at the temporary solution it is in the gameEngine function in the if(falling) statement
// Imports
import flash.events.KeyboardEvent;
import flash.events.Event;
// Constants that can be edited
const grav:Number = 0.05;
const jumpPow:Number = 15;
const walkingDistance = 50;
// Variables
var jumpVel:Number = jumpPow;
var velocity:Number = 0;
var dt:Number = 0;
var movingCount:Number = 0;
var newX:Number = 0;
var newY:Number = 0;
var charCornerHit:Number = 0; // 0 = TL 1 = TR 2 = BR 3 = BL (clockwise)
var jumping:Boolean = false;
var falling:Boolean = false;
var movingRight:Boolean = false;
var movingLeft:Boolean = false;
var hitDetVal:Boolean = false; // false
var floorHit:Boolean = false;
var object1Hit:Boolean = false;
var cCnrTL:Array = new Array(char.x, char.y);
var cCnrTR:Array = new Array(char.x + char.width, char.y);
var cCnrBR:Array = new Array(char.x + char.width, char.y + char.height);
var cCnrBL:Array = new Array(char.x, char.y + char.height);
var charCorners:Array = new Array(cCnrTL, cCnrTR, cCnrBR, cCnrBL); // Clockwise
addEventListener(Event.ENTER_FRAME, gameEngine);
function gameEngine(evt:Event):void{
stage.addEventListener(KeyboardEvent.KEY_DOWN, moveCharacter);
charCorners[0][0] = char.x;
charCorners[0][1] = char.y;
charCorners[1][0] = char.x + char.width;
charCorners[1][1] = char.y;
charCorners[2][0] = char.x + char.width;
charCorners[2][1] = char.y + char.height;
charCorners[3][0] = char.x;
charCorners[3][1] = char.y + char.height;
//trace(char.y);
// Check if char is standing on something
// Supposed to only check when character is not falling
if (falling == false) {
//trace(char.y + char.height + 1)
hitDetection(0, char.y + char.height + 1)
if (hitDetVal == false) {
falling = true;
hitDetVal = false;
trace("not standing");
}
else {trace("standing on something");}
}
// Move char loop
if (movingRight){
if (movingCount == walkingDistance) {
movingCount = 0
movingRight = false;
} else {
char.x+=1
movingCount += 1;
}
} else if (movingLeft) {
if (movingCount == walkingDistance) {
movingCount = 0
movingLeft = false;
} else {
char.x-=1
movingCount += 1;
}
}
//trace(velocity)
if (falling) {
dt += 1
velocity = velocity + grav*dt
hitDetection(0, velocity)
if (hitDetVal) {
falling = false;
hitDetVal = false;
// TEMPORARY SOLUTION - Stopped character from falling past the floor but still ran falling condition
//velocity = 0;
//dt = 0;
if (floorHit) {
if (char.y < floor.y){
char.y = floor.y - char.height;
floorHit = false;
trace("Char Stopped falling")
}
} else if (object1Hit) {
if (char.y - char.height < object1.y){
char.y = object1.y - char.height;
object1Hit = false;
trace("Char Stopped falling")
}
}
} else {
char.y += velocity;
}
} else {
if (jumping) {
}
velocity = 0;
dt = 0;
}
}
function moveCharacter(event:KeyboardEvent):void{
if(event.keyCode == Keyboard.LEFT){
if(movingRight){
movingRight = false;
trace("STOPPED")
} else {
movingCount = 0
movingLeft = true;
trace("LEFT");
}
}
if(event.keyCode == Keyboard.RIGHT){
if(movingLeft){
movingLeft = false;
trace("STOPPED")
} else {
movingCount = 0
movingRight = true;
trace("RIGHT");
}
}
if(event.keyCode == Keyboard.UP){
jumping = true;
trace("UP");
}
}
function hitDetection(distX:Number, distY:Number) {
for (var i:uint = 0; i < charCorners.length; i++) {
newX = charCorners[i][0] + distX;
newY = charCorners[i][1] + distY;
if (floor.hitTestPoint(newX, newY, true)){
hitDetVal = true;
floorHit = true;
charCornerHit = i;
break;
} else if (object1.hitTestPoint(newX, newY, true)){
hitDetVal = true;
object1Hit = true;
charCornerHit = i;
break;
}
}
}```
It was a simple logic error occurring here
hitDetection(0, char.y + char.height + 1)
I had made adjustments for the location by adding char.y to the value when my hitDetection function already made an adjustment to each corner of my character. Only took ~3 hours to figure out

Actionscript 3 error 1009: Cannot access a property or method of a null object reference

I'm trying to make a simple game on Animate CC. Everything seems to work fine except when I look in the output, I get the following error:
TypeError: Error #1009: Cannot access a property or method of a null
object reference.
at _2D_CW2_Game_v10_8_fla::MainTimeline/move()
at _2D_CW2_Game_v10_8_fla::MainTimeline/updateOb()
So I know where the issue might be, and I've been trying tweaking the code for days, googling possible solutions, but to no avail...
My entire source code is as below. Any feedback/suggestions will be greatly appreciated.
import flash.ui.Keyboard;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.display.MovieClip;
import flash.net.URLRequest;
import flash.media.Sound;
import flash.media.SoundMixer;
//==================================================
// Variable declaration
//==================================================
// defines the variables for boundaries
var left:Number = 0;
var top:Number = 0;
var right:Number = stage.stageWidth;
var bottom:Number = stage.stageHeight;
var velX:Number = 0;
var velY:Number = 0;
var gravity:Number = 1;
var friction:Number = 0.8;
var bounce:Number = -0.5;
var score:Number = 2;
var cv:Number = 0;
var curCount:Number = 30; // countdown 30s
var rightKeyDown:Boolean = false;
var leftKeyDown:Boolean = false;
var upKeyDown:Boolean = false;
var touchGround:Boolean = false;
// create and place player object on stage
var player:Player = new Player();
player.x = 110;
player.y = 460;
addChild(player);
// create obstacle array
var obstacles:Array = new Array();
var numOb:Number = 3;
// create and place enemies on stage
for (var i:Number = 0; i < numOb; i++) {
var ob:Npc = new Npc();
ob.x = 800;
ob.y = 470;
ob.scaleX = -1;
ob.vx = Math.random() * 20 + 1;
addChild(ob);
obstacles.push(ob);
}
//==================================================
// Event handlers
//==================================================
stage.addEventListener(Event.ENTER_FRAME, EntFrame);
addEventListener(Event.ENTER_FRAME, updateOb);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUp);
//==================================================
// Functions
//==================================================
function keyDown(e:KeyboardEvent):void {
if (e.keyCode == Keyboard.RIGHT) {
rightKeyDown = true;
}
if (e.keyCode == Keyboard.LEFT) {
leftKeyDown = true;
}
if (e.keyCode == Keyboard.UP) {
// if player isn't already jumping and is on the ground
if (!upKeyDown && touchGround) {
// then start jumping
isJumping();
}
upKeyDown = true;
}
}
function keyUp(e:KeyboardEvent):void {
if (e.keyCode == Keyboard.RIGHT) {
rightKeyDown = false;
}
if (e.keyCode == Keyboard.LEFT) {
leftKeyDown = false;
}
if (e.keyCode == Keyboard.UP) {
upKeyDown = false;
}
}
function EntFrame(e:Event):void {
player.x += velX;
player.y += velY;
velX *= friction;
velY += gravity;
if (player.y >= 450) {
touchGround = true;
player.y = 450;
}
// boundary checks
if (player.x + player.width/2 > right) {
player.x = right - player.width/2;
player.velX *= bounce;
} else if (player.x - player.width/2 < left) {
player.x = left + player.width/2;
player.velX *= bounce;
}
// make player move left or right
controls();
if (curCount > 0) {
cv++;
if (cv >= 30) {
curCount--;
cv = 0;
timertext.text = String(curCount);
if (curCount == 0) {
restart();
gotoAndStop("gameOverWon");
}
}
}
}
function updateOb(e:Event):void {
// make obstacles move
for (var i:Number = 0; i < numOb; i++) {
var ob:Npc = obstacles[i];
move(ob);
if (player.hitTestObject(obstacles[i])) {
/*if (obstacles[i].hitTestPoint(player.x + player.width/2, player.y + player.height/2, true)
|| obstacles[i].hitTestPoint(player.x + player.width/2, player.y - player.height/2, true)
|| obstacles[i].hitTestPoint(player.x - player.width/2, player.y + player.height/2, true)
|| obstacles[i].hitTestPoint(player.x - player.width/2, player.y - player.height/2, true))*/
bumpOb(obstacles[i]);
}
}
scoretext.text = String(score);
if (score == 0) {
restart();
gotoAndStop("gameOverLost");
}
}
// applies basic velocity to enemies
function move(moveOb) {
moveOb.x -= moveOb.vx;
if (moveOb.x + moveOb.width/2 > right) {
moveOb.x = right - moveOb.width/2;
moveOb.vx *= bounce;
moveOb.scaleX = -1;
}
if (moveOb.x - moveOb.width/2 < left) {
moveOb.x = left + moveOb.width/2;
moveOb.vx *= bounce;
moveOb.scaleX = 1;
}
}
function bumpOb(p) {
if (p) {
p.removeEventListener(Event.ENTER_FRAME, updateOb);
if (p.parent) {
removeChild(p);
score--;
}
}
}
function restart() {
if(contains(player)) {
removeChild(player);
}
for (var i:int = 0; i < numOb; i++) {
if (contains(obstacles[i]) && obstacles[i] != null) {
removeChild(obstacles[i]);
obstacles[i] = null;
}
}
// returns a new array that consists of a range of elements from the original array,
// without modifying the original array
obstacles.slice(0);
}
function controls() {
if (rightKeyDown) {
velX += 3;
player.scaleX = 1;
}
if (leftKeyDown) {
velX -= 3;
player.scaleX = -1;
}
}
function isJumping() {
touchGround = false;
velY = -15;
}
//==================================================
// Sound control for background music
//==================================================
btnMute.addEventListener(MouseEvent.CLICK, mute);
function mute(e:MouseEvent):void {
SoundMixer.soundTransform = new SoundTransform(0);
btnMute.removeEventListener(MouseEvent.CLICK, mute);
btnMute.addEventListener(MouseEvent.CLICK, unmute);
}
function unmute(e:MouseEvent):void {
SoundMixer.soundTransform = new SoundTransform(1);
btnMute.removeEventListener(MouseEvent.CLICK, unmute);
btnMute.addEventListener(MouseEvent.CLICK, mute);
}
I had the same problem when I created interactive elements for animation. Check which layer the interactive object is on. A similar error occurred when the object overlapped something that was located on the layer above.
You can try...
1) Your Npc is a class/library object, right?
Give the source MC/Sprite, the instance name of moveOb or p.
2) or else try... Use function parameters (this is a better coding style):
(2a) Since you say..
var ob:Npc = obstacles[i];
move(ob);
ps: why not simplify (without var) as : move( obstacles[i] ); ...?
(2b) Your move function should specify a data type together with your parameter name...
//# applies basic velocity to enemies
//# Wrong...
//function move(moveOb) {
//# Better...
function move( moveOb : Npc ) {
//# Aso fix as...
function bumpOb(p : Npc ) {
By using function parameters, you can now give unique names to the (function's) input parameters but stay referencing the same (or compatible) data type.
Let me know how it goes.
The obstacles array may have null elements in the middle. What if you add a condition to continue if it's null?
function updateOb(e:Event):void {
// make obstacles move
for (var i:Number = 0; i < obstacles.length; i++) {
var ob:Npc = obstacles[i];
if(!ob) continue;
move(ob);
if (player.hitTestObject(ob)) {
/*if (obstacles[i].hitTestPoint(player.x + player.width/2, player.y +
player.height/2, true)
|| obstacles[i].hitTestPoint(player.x + player.width/2, player.y -
player.height/2, true)
|| obstacles[i].hitTestPoint(player.x - player.width/2, player.y +
player.height/2, true)
|| obstacles[i].hitTestPoint(player.x - player.width/2, player.y -
player.height/2, true))*/
bumpOb(obstacles[i]);
}
}
scoretext.text = String(score);
if (score == 0) {
restart();
gotoAndStop("gameOverLost");
}
}

error #1009, even after removing event listeners

In a flappy bird game I'm making, I previously got a problem of error #1009. I solved some of it by the removeEventListeners while toggling between frames but there are still some of the problems.Here's my file.. Please help.
The problem's are seen in the slide, obstposi and range functions. The codes are here:
slide AND obstposi functions:
var start = false;
stage.addEventListener(Event.ENTER_FRAME, slide);
stage.addEventListener(Event.ENTER_FRAME, obstposi);
function slide(event: Event) {
if (dead != true) {
p1.x -= 5;
p2.x -= 5;
p3.x -= 5;
p4.x -= 5;
p5.x -= 5;
p6.x -= 5;
p7.x -= 5;
p8.x -= 5;
scorein1.x -= 5;
scorein2.x -= 5;
scorein3.x -= 5;
scorein4.x -= 5;
}
if (flappy.hitTestObject(p1)) {
dead = true;
} else if (flappy.hitTestObject(p2)) {
dead = true;
} else if (flappy.hitTestObject(p3)) {
dead = true;
} else if (flappy.hitTestObject(p4)) {
dead = true;
} else if (flappy.hitTestObject(p5)) {
dead = true;
} else if (flappy.hitTestObject(p6)) {
dead = true;
} else if (flappy.hitTestObject(p7)) {
dead = true;
} else if (flappy.hitTestObject(p8)) {
dead = true;
}
}
function obstposi(event: Event) {
if (flappy.hitTestObject(scorein4)) {
scorein1.x = 702.3;
p1.x = 650;
p2.x = 650;
p1.y = Math.ceil(Math.random() * 300) + 305;
p2.y = p1.y - 335;
start = true;
} else if (flappy.hitTestObject(scorein1) && start == true) {
scorein2.x = 702.3;
p3.x = 650;
p4.x = 650;
p4.y = Math.ceil(Math.random() * -300) + 305;
p3.y = p4.y - 335;
} else if (flappy.hitTestObject(scorein2) && start == true) {
scorein3.x = 702.3;
p5.x = 650;
p6.x = 650;
p6.y = Math.ceil(Math.random() * -300) + 305;
p5.y = p6.y - 335;
} else if (flappy.hitTestObject(scorein3) && start == true) {
scorein4.x = 702.3;
p7.x = 650;
p8.x = 650;
p8.y = Math.ceil(Math.random() * -300) + 305;
p7.y = p8.y - 335;
}
}
range function:
stage.addEventListener(Event.ENTER_FRAME, range);
function range(event: Event) {
if (limit.hitTestObject(flappy)) {
flappy.x -= 2.5;
}
else if (wall.hitTestObject(flappy)) {
flappy.y += 20;
}
}
The problem of your code is that you made the function work even if the objects like flappy, ground, etc. are not present in your scene. So, before starting you can add if(flappy != null) to make your code work only when flappy is present in the stage.

AS3 - Restart Loop Error

I have now figured out that After I restart my game The Enter_frame function is not looping? why is that.
Enter_Frame:
public function enter_frame(e:Event)
{
trace("rGame = " + rGame);
_Menu();
if(mGameStart)
{
//Stage & Players
_PlayerStart();
_PlayerCon();
_MoveStage();
}
if(rGame)
{
_PlayerCon();
trace("_PlayerCon(): Done");
_MoveStage();
}
if(Game_Over)
{
mGameStart = false;
GameOver();
}
}
Pipe_Spawn:
private function eSpawn()
{
for(var i:int = 0; i < ePipeMax; i++)
{
var _Pipe:EPipe = new EPipe;
_Pipe.x = (i * 250) + 640;
_Pipe.y = Math.random() * 90;
PipeLayer.addChild(_Pipe);
}
}
Pipe_Move:
public function _MoveStage():void
{
//Pipes Move
for (var i:int; i < vPipeMax; i++)
{
var _Pipe = PipeLayer.getChildAt(i);
if(_Pipe.hitTestPoint(_Player.x, _Player.y, true))
{
blink = true;
Remove(_White);
Game_Over = true;
}
else
{
if(_Pipe.x < 241 && _Pipe.x > 234){
ScoreReady = true;
}
else
{
ScoreReady = false;
two = true;
}
if(ScoreReady && two)
{
Score++;
Scores.text = Score.toString();
two = false;
}
if(_Pipe.x <= -20)
{
_Pipe.x = 640 + 100;
_Pipe.y = Math.random()*90;
}
_Pipe.x -= xSpeed;
}
}
}
Game_Restart:
public function restartButton(m:MouseEvent)
{
GOSign.y = 1000;
R_Button.y = 1000;
Scores.y = 105.65;
HighScore.x = 500;
ScoreReady = false;
two = true;
HighScore5();
_Player.x = 240;
_Player.y = 320;
//This is where I restart the position of the pipes
for (var i:int = 0; i < vPipeMax; i++)
{
var _Pipe = PipeLayer.getChildAt(i);
_Pipe.x = (i * 250) + 640;
_Pipe.y = Math.random() * 90;
}
rGame = true;
}
Iv been at this for about two weeks and can not fix it. Thank you for reading.
I have found my problem. It was a VERY stupid mistake and I cant believe it took me this long to figure it out. I forgot to reset a variable ~facepalm~. I guess you learn from your mistakes lol

Got lost on this code and cant make it work

Im getting an weird error about constructions definitions that i have no idea what means.
Im studing this for a test.I think the problem is on the functions but im not sure.
Its a code from an game (pretty simple one) that you have to move your player from point b to point a without letting the falling asteroids touch you.
Thanks in advance.
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.events.Event;
var up:Boolean;
var down:Boolean;
var left:Boolean;
var right:Boolean;
var vel:Number;
var velInimigo:Number;
var sentidoX:Number;
var sentidoY:Number;
var d:MovieClip;
var txt1:MovieClip;
var chegada:MovieClip;
var player:MovieClip;
var inimigos:Array;
var i:int;
var mc:MovieClip;
function MainTimeline()
{
addFrameScript(0, frame1);
return;
}
function OnKeyDown(event:KeyboardEvent) : void
{
switch(event.keyCode)
{
case Keyboard.W:
{
up = true;
break;
}
case Keyboard.S:
{
down = true;
break;
}
case Keyboard.A:
{
left = true;
break;
}
case Keyboard.D:
{
right = true;
break;
}
default:
{
break;
}
}
return;
}
function OnKeyUp(event:KeyboardEvent) : void
{
switch(event.keyCode)
{
case Keyboard.W:
{
up = false;
break;
}
case Keyboard.S:
{
down = false;
break;
}
case Keyboard.A:
{
left = false;
break;
}
case Keyboard.D:
{
right = false;
break;
}
default:
{
break;
}
}
return;
}
function OnFrame(event:Event) : void
{
var loc3: = null;
var loc4: = null;
if (up)
{
sentidoY = -1;
}
else if (down)
{
sentidoY = 1;
}
else
{
sentidoY = 0;
}
if (left)
{
sentidoX = -1;
}
else if (right)
{
sentidoX = 1;
}
else
{
sentidoX = 0;
}
if (sentidoX == 1 && sentidoY == 0)
{
player.rotation = 0 + 90;
}
else if (sentidoX == 1 && sentidoY == 1)
{
player.rotation = 45 + 90;
}
else if (sentidoX == 0 && sentidoY == 1)
{
player.rotation = 90 + 90;
}
else if (sentidoX == -1 && sentidoY == 1)
{
player.rotation = 135 + 90;
}
else if (sentidoX == -1 && sentidoY == 0)
{
player.rotation = 180 + 90;
}
else if (sentidoX == -1 && sentidoY == -1)
{
player.rotation = 225 + 90;
}
else if (sentidoX == 0 && sentidoY == -1)
{
player.rotation = 270 + 90;
}
else if (sentidoX == 1 && sentidoY == -1)
{
player.rotation = 315 + 90;
}
player.x = player.x + vel * sentidoX;
player.y = player.y + vel * sentidoY;
var loc2: = 0 ;
while (loc2 < inimigos.length)
{
loc3 = inimigos[loc2];
loc3.y = loc3.y + loc3.vel;
loc3.rotation = loc3.rotation + loc3.rotVel;
if (loc3.hitTestObject(player))
{
loc4 = new GameOverMC();
stage.addChild(loc4);
loc4.x = stage.stage.width / 2;
loc4.y = stage.stageHeight / 2;
stage.removeEventListener(Event.ENTERFRAME, OnFrame);
}
if (loc3.y > stage.stageHeight)
{
loc3.y = -loc3.height;
}
loc2++;
}
if (player.hitTestObject(chegada))
{
if (txt1 == null)
{
txt1 = new VitoriaMC();
stage.addChild(txt1);
txt1.x = stage.stage.width / 2;
txt1.y = stage.stageHeight / 2;
}
}
d.x = d.x + d.vel * d.sx;
d.y = d.y + d.vel * d.sy;
if (d.x >= stage.stageWidth - d.width / 2 || d.x <= d.width / 2)
{
d.sx = -d.sx;
}
if (d.y >= stage.stageHeight - d.height / 2 || d.y <= d.height / 2)
{
d.sy = -d.sy;
}
return;
}
function frame1()
{
vel = 5;
velInimigo = 2;
sentidoX = 0;
sentidoY = 0;
d = new DemoMC();
d.vel = 1;
d.sx = 1;
d.sy = 1;
d.x = stage.stageWidth / 2;
d.y = stage.stageHeight / 2;
txt1 = null;
chegada = new ChegadaMC();
player = new PlayerMC();
inimigos = new Array();
stage.addChild(d);
stage.addChild(chegada);
stage.addChild(player);
player.x = player.width / 2;
player.y = stage.stageHeight - player.height / 2;
chegada.x = stage.stageWidth - chegada.width;
chegada.y = 0;
i = 0;
while (i < 60)
{
mc = new AstroMC();
stage.addChild(mc);
inimigos.push(mc);
mc.x = 100 + Math.random() * (600 - mc.width);
mc.y = Math.random() * stage.stageHeight;
mc.rotVel = Math.random() * 10 - 5;
mc.vel = Math.random() * 3 + 1;
var loc1: = i;
var loc2: = i + 1;
loc1.i = loc2;
}
stage.addEventListener(KeyboardEvent.KEYDOWN, OnKeyDown);
stage.addEventListener(KeyboardEvent.KEYUP, OnKeyUp);
stage.addEventListener(Event.ENTERFRAME, OnFrame);
return;
}
It has been awhile since i have played with AS3 but i see a few odd things one being the
loc1.i = loc2;
near the bottom
what is "i" in relation to loc1? and this is in a while loop using i but never increments it so it seems like an infinite loop
again i could be wrong its been quite some time.