2 same errors in one script - actionscript-3

I have 2 errors:
TypeError: Error #1009: Cannot access a property or method of a null
object reference. at
BlowfishPong_fla::MainTimeline/countTime()[BlowfishPong_fla.MainTimeline::frame79:75]
TypeError: Error #1009: Cannot access a property or method of a null
object reference. at
BlowfishPong_fla::MainTimeline/airScore()[BlowfishPong_fla.MainTimeline::frame79:131]
Look at this code:
timedScore = 0;
var goalScore: int = Math.floor(Math.random() * 101) + 20;
var speedSeconds = 0;
var speedMinutes = 0;
keyNum = 0;
var OxygenTime = 0;
var OxygenMaxTime = 5;
var goalKey: int = Math.floor(Math.random() * 10) + 3;
var ballSpeedXTimed: int = -3;
var ballSpeedYTimed: int = -2;
var cpuPaddleSpeedTimed: int = 3;
stopwatch.play();
oxygenGauge.stop();
var FiftyTimed: Boolean = false;
var plzStopTimed: Boolean = false;
helpContent_Timed.visible = false;
stopwatch.addEventListener(Event.ENTER_FRAME, countTime);
oxygenGauge.addEventListener(Event.ENTER_FRAME, airScore);
goalScore_txt.text = String(goalScore);
timedScore_txt.text = timedScore;
key_txt.text = keyNum + "/" + goalKey;
stage.addEventListener(Event.ENTER_FRAME, loopTimed);
function updateTextFieldsTimed(): void {
goalScore_txt.text = String(goalScore);
timedScore_txt.text = timedScore;
key_txt.text = keyNum + "/" + goalKey;
}
function calculateBallAngleTimed(paddleY: Number, ballY: Number): Number {
var ySpeed: Number = 5 * ((ballY - paddleY) / 25);
return ySpeed;
}
function countTime(e: Event): void {
if (stopwatch.currentFrame == 61) {
speedSeconds++;
if (speedSeconds > 59) {
speedSeconds = 0;
speedtimerSec_txt.text = "0" + speedSeconds;
speedMinutes++;
if (speedMinutes > 10) {
speedtimerMin_txt.text = "" + speedMinutes;
} else {
speedtimerMin_txt.text = "0" + speedMinutes;
}
if (speedMinutes > 59) {
stopwatch.stop();
gotoAndPlay("gameover_Timed");
}
} else {
if (speedSeconds >= 10) {
speedtimerSec_txt.text = "" + speedSeconds;
} else {
speedtimerSec_txt.text = "0" + speedSeconds;
}
}
}
}
function airScore(timedScore): void {
if (timedScore == 50 && FiftyTimed == false) {
character_TiedUp.gotoAndStop(2);
FiftyTimed = true;
oxygenGauge.play();
OxygenTime++;
} else if (timedScore == 150 && FiftyTimed == false) {
character_TiedUp.gotoAndStop(2);
FiftyTimed = true;
oxygenGauge.play();
oxygenGauge.frameRate = 1.5;
OxygenTime++;
} else if (timedScore == 300 && FiftyTimed == false) {
character_TiedUp.gotoAndStop(2);
FiftyTimed = true;
oxygenGauge.play();
OxygenTime++;
} else if (timedScore == 450 && FiftyTimed == false) {
character_TiedUp.gotoAndStop(2);
FiftyTimed = true;
oxygenGauge.play();
OxygenTime++;
} else if (timedScore == 600 && FiftyTimed == false) {
character_TiedUp.gotoAndStop(2);
FiftyTimed = true;
oxygenGauge.play();
OxygenTime++;
}
if (oxygenGauge.currentFrame == 505) {
character_TiedUp.gotoAndStop(3);
oxygenGauge.stop();
oxygenGauge.gotoAndStop(1);
}
}
function loopTimed(event: Event): void {
if (plzStopTimed == false) {
playerPaddle.y = mouseY;
keyPong.x += ballSpeedXTimed;
keyPong.y += ballSpeedYTimed;
//check left and right boundaries
if (keyPong.x <= keyPong.width / 2) {
keyPong.x = keyPong.width / 2;
ballSpeedXTimed *= -1;
keyNum++;
BingTimed.play();
if (goalKey == keyNum) {
key_txt.textColor = 0x00FF00;
}
updateTextFieldsTimed();
} else if (keyPong.x >= stage.stageWidth - keyPong.width / 2) {
keyPong.x = stage.stageWidth - keyPong.width / 2;
ballSpeedXTimed *= -1;
BingTimed.play();
if (keyNum > 0) {
keyNum--;
}
if(goalKey < keyNum) {
key_txt.textColor = 0xFFFFFF;
}
updateTextFieldsTimed();
}
if (keyPong.y <= keyPong.height / 2) {
keyPong.y = keyPong.height / 2;
ballSpeedYTimed *= -1;
gameBounceTimed.play();
} else if (keyPong.y >= stage.stageHeight - keyPong.height / 2) {
keyPong.y = stage.stageHeight - keyPong.height / 2;
ballSpeedYTimed *= -1;
gameBounceTimed.play();
}
if (cpuPaddle.y < keyPong.y - 10) {
cpuPaddle.y += cpuPaddleSpeedTimed;
} else if (cpuPaddle.y > keyPong.y + 10) {
cpuPaddle.y -= cpuPaddleSpeedTimed;
}
if (playerPaddle.y - playerPaddle.height / 4 < 0) {
playerPaddle.y = playerPaddle.height / 4;
} else if (playerPaddle.y + playerPaddle.height / 4 > stage.stageHeight) {
playerPaddle.y = stage.stageHeight - playerPaddle.height / 4;
}
if (playerPaddle.paddle.hitTestObject(keyPong) == true) {
if (ballSpeedXTimed > 0) {
ballSpeedXTimed *= -1;
ballSpeedYTimed = calculateBallAngleTimed(playerPaddle.y, keyPong.y);
timedScore++
gameHitTimed.play();
airScore(timedScore);
if (goalKey == keyNum) {
if (goalScore == timedScore) {
stage.removeEventListener(Event.ENTER_FRAME, loopTimed);
gotoAndStop("gameover_Timed");
return;
}
}
updateTextFieldsTimed();
}
} else if (cpuPaddle.paddle.hitTestObject(keyPong) == true) {
if (ballSpeedXTimed < 0) {
ballSpeedXTimed *= -1;
ballSpeedYTimed = calculateBallAngleTimed(cpuPaddle.y, keyPong.y);
timedScore++;
gameHitTimed.play();
airScore(timedScore);
if (goalKey == keyNum) {
if (goalScore == timedScore) {
stage.removeEventListener(Event.ENTER_FRAME, loopTimed);
gotoAndStop("gameover_Timed");
return;
}
}
updateTextFieldsTimed();
}
}
}
}
Can anyone how to fix these?

Related

Error #2025 twice

package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.ui.Keyboard;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
public class supportForce extends MovieClip
{
private var Player1Child:Player1Actual = new Player1Actual();
private var Player2Child:Player2Actual = new Player2Actual();
private var GreenLightLeft:Projectile1 = new Projectile1();
private var GreenLightRight:Projectile2 = new Projectile2();
private var NeonChild:mcNeonPlayer = new mcNeonPlayer();
private var _keyDownStatus:Object = {};
private var defaultSpeed:int = 10;
private var Player1Lock:Boolean = false;
private var Player2Lock:Boolean = false;
private var Player1Left:Boolean = false;
private var Player2Left:Boolean = true;
private var greenLightLeft:Boolean = true;
private var Player1CD:int = 0;
private var Player1Ready:Boolean = true;
private var Player1Hit:Boolean = false;
private var Player1X:int;
private var Player1Y:int;
private var NeonX:int;
private var NeonY:int;
private var NeonCD:int = 0;
private var NeonDuration:int = 240;
private var NeonUse:Boolean = false;
public function supportForce()
{
this.addEventListener(Event.ENTER_FRAME, general);
stage.addEventListener(KeyboardEvent.KEY_DOWN, onDown);
stage.addEventListener(KeyboardEvent.KEY_UP, onUp);
stage.addEventListener(Event.ENTER_FRAME, keyCheck);
btnStart.addEventListener(MouseEvent.CLICK, clickStart);
btnAbout.addEventListener(MouseEvent.CLICK, clickAbout);
btnDust.addEventListener(MouseEvent.CLICK, clickDust);
}
private function general(event:Event)
{
Player1X = Player1Child.x;
Player1Y = Player1Child.y;
NeonX = NeonChild.x;
NeonY = NeonChild.y;
GreenLightLeft.x -= (defaultSpeed * 2);
GreenLightRight.x += (defaultSpeed * 2);
//status_text = NeonCD;
if (NeonDuration > 0 && NeonUse == true)
{
NeonDuration -= 1;
}
else if (NeonDuration == 0 && NeonUse == true)
{
NeonUse = false;
removeChild(NeonChild);
addChild(Player1Child);
Player1Child.x = NeonX;
Player1Child.y = NeonY;
}
if (NeonCD > 0)
{
NeonCD -= 1;
}
trace(NeonCD);
if (Player1Child.BladeInstance.hitTestObject(Player2Child) && Player1CD <= 0 && Player1Child.BladeInstance.alpha != 0)
{
if (Player1Left == true)
{
Player2Child.x -= defaultSpeed;
Player1Hit = true;
Player1CD = 8;
//trace("G");
}
if (Player1Left == false)
{
Player2Child.x += defaultSpeed;
Player1Hit = true;
Player1CD = 8;
//trace("G");
}
}
if (Player1Ready == false)
{
Player1CD -= 1;
}
if (Player1CD <= 0)
{
Player1Ready = true;
Player1CD = 0;
}
else
{
Player1Ready = false;
}
if (Player1Left == true)
{
if (Player1Child.x <= (0 + Player1Child.width))
{
Player1Child.x = (0 + Player1Child.width);
}
if (Player1Child.x >= 550)
{
Player1Child.x = 550;
}
if (Player1Child.y >= 400)
{
Player1Child.y = 400;
}
if (Player1Child.y <= (0 + Player1Child.height))
{
Player1Child.y = (0 + Player1Child.height);
}
}
else if (Player1Left == false)
{
if (Player1Child.x <= 0)
{
Player1Child.x = 0;
}
if (Player1Child.x >= (550 - Player1Child.width))
{
Player1Child.x = (550 - Player1Child.width);
}
if (Player1Child.y <= 0)
{
Player1Child.y = 0;
}
if (Player1Child.y >= (400 - Player1Child.height))
{
Player1Child.y = (400 - Player1Child.height);
}
}
if (Player2Child.x <= Player2Child.width)
{
Player2Child.x = Player2Child.width;
}
if (Player2Child.x >= 550)
{
Player2Child.x = 550;
}
if (Player2Child.y >= (400 - Player2Child.height))
{
Player2Child.y = 400 - Player2Child.height;
}
if (Player2Child.y <= 0)
{
Player2Child.y = 0;
}
}
private function onUp(e:KeyboardEvent):void
{
_keyDownStatus[e.keyCode] = false;
}
private function onDown(e:KeyboardEvent):void
{
_keyDownStatus[e.keyCode] = true;
}
private function keyCheck(event:Event)
{
if (_keyDownStatus[37])
{
if (Player1Left == false)
{
Player1Left = true;
Player1Child.rotation += 180;
Player1Child.y += (Player1Child.height - 5);
Player1Child.x += Player1Child.width;
}
Player1Child.x -= defaultSpeed;
}
if (_keyDownStatus[39])
{
if (Player1Left == true)
{
Player1Left = false;
Player1Child.rotation += 180;
Player1Child.y -= (Player1Child.height - 5);
Player1Child.x -= Player1Child.width;
}
Player1Child.x += defaultSpeed;
}
if (_keyDownStatus[38])
{
Player1Child.y -= defaultSpeed;
}
if (_keyDownStatus[40])
{
Player1Child.y += defaultSpeed;
}
if (_keyDownStatus[96] && Player1Ready == true)
{
Player1Child.play();
Player1Ready = false;
}
if (_keyDownStatus[97] && NeonCD <= 0)
{
removeChild(Player1Child);
stage.addChild(NeonChild);
NeonChild.x = Player1X;
NeonChild.y = Player1Y;
NeonCD = 480;
NeonUse = true;
Player1Lock = false;
}
if (_keyDownStatus[65])
{
if (Player2Left == false)
{
Player2Left = true;
Player2Child.rotation -= 180;
Player2Child.y -= (Player2Child.height - 5);
Player2Child.x -= Player2Child.width;
}
Player2Child.x -= defaultSpeed;
}
if (_keyDownStatus[68])
{
if (Player2Left == true)
{
Player2Left = false;
Player2Child.rotation += 180;
Player2Child.y += (Player2Child.height - 5);
Player2Child.x += Player2Child.width;
}
Player2Child.x += defaultSpeed;
}
if (_keyDownStatus[87])
{
Player2Child.y -= defaultSpeed;
}
if (_keyDownStatus[83])
{
Player2Child.y += defaultSpeed;
}
if (_keyDownStatus[90])
{
if (Player2Left == true)
{
var GreenLightLeft:Projectile1 = new Projectile1();
stage.addChild(GreenLightLeft);
GreenLightLeft.x = Player2Child.x;
GreenLightLeft.y = Player2Child.y;
greenLightLeft = true;
}
if (Player2Left == false)
{
var GreenLightRight:Projectile2 = new Projectile2();
stage.addChild(GreenLightRight);
GreenLightRight.x = (Player2Child.x);
GreenLightRight.y = (Player2Child.y - Player2Child.height);
greenLightLeft = false;
}
}
}
private function clickStart(event:MouseEvent):void
{
gotoAndStop(2);
}
private function clickAbout(event:MouseEvent):void
{
}
private function clickDust(event:MouseEvent):void
{
}
}
}
It works perfectly when I press Numpad 1 (97) but the second time, it gives this error.
ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
at flash.display::DisplayObjectContainer/removeChild()
at supportForce/keyCheck()
So I planned of removeing the NeonChild after 10 seconds or 240 frames but it gives this error.
ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
at flash.display::DisplayObjectContainer/removeChild()
at supportForce/general()
If you cannot understand what I am trying to say, this is the link to the actual .fla and .as
files
if you dont know parent of displayObject, which you want to remove you can write
mc.parent.removeChilte(mc);
where mc is movieclip , which you want to delete
Use this instead
this.parent.removeChild(mc);
where mc is the MovieClip.

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.

Button only clickable when visible

Ok i want my button to only be clickabe once it is visibe, it is invisible til you win the game(1 score of pong)
here is what i have
var buttonsStates:Object = {
"scoreBoard_W" : false
};
function checkVisibility () {
for (var scoreBoard_W:String in buttonsStates) {
if(visible == true)
{
scoreBoard_W.addEventListener(MouseEvent.CLICK, goto3);
function goto3(Event:MouseEvent)
{
gotoAndStop(1,"Menu");
}
and here is the error: Pong, Layer 'Pong', Frame 2, Line 129 1061: Call to a possibly undefined method addEventListener through a reference with static type String.
im not sure what it means, or if im on the right track any help is apperciaed
Here is all of the code
stop();
var buttonsStates:Object = {
"scoreBoard_W" : false
};
var ballSpeedX:int = -3;
var ballSpeedY:int = -2;
var cpuPaddleSpeed:int = 3;
var playerScore:int = 0;
var cpuScore:int = 0;
scoreBoard_W.visible = false;
scoreBoard_L.visible = false;
init();
function init():void
{
stage.addEventListener(Event.ENTER_FRAME, loop);
}
function calculateBallAngle(paddleY:Number, ballY:Number):Number
{
var ySpeed:Number = 5 * ((ballY - paddleY) / 25);
return ySpeed;
}
function updateTextFields():void
{
playerScoreText.text = ("Player Score: " + playerScore);
cpuScoreText.text = ("CPU Score: " + cpuScore);
}
function loop(e:Event):void
{
if (playerPaddle.hitTestObject(ball) == true)
{
if (ballSpeedX < 0)
{
ballSpeedX *= -1;
ballSpeedY = calculateBallAngle(playerPaddle.y, ball.y);
}
}
else if (cpuPaddle.hitTestObject(ball) == true )
{
if (ballSpeedX > 0)
{
ballSpeedX *= -1;
ballSpeedY = calculateBallAngle(cpuPaddle.y, ball.y);
}
}
if (cpuPaddle.y < ball.y - 10)
{
cpuPaddle.y += cpuPaddleSpeed;
}
else if (cpuPaddle.y > ball.y + 10)
{
cpuPaddle.y -= cpuPaddleSpeed;
}
playerPaddle.y = mouseY;
if (playerPaddle.y - playerPaddle.height / 2 < 0)
{
playerPaddle.y = playerPaddle.height / 2;
}
else if (playerPaddle.y + playerPaddle.height/2 > stage.stageHeight)
{
playerPaddle.y = stage.stageHeight - playerPaddle.height / 2;
}
ball.x += ballSpeedX;
ball.y += ballSpeedY;
if (ball.x <= ball.width / 2)
{
ball.x = ball.width / 2;
ballSpeedX *= -1;
cpuScore++;
updateTextFields();
}
else if (ball.x >= stage.stageWidth-ball.width/2)
{
ball.x = stage.stageWidth - ball.width / 2;
ballSpeedX *= -1;
playerScore++;
updateTextFields();
}
if (ball.y <= ball.height / 2)
{
ball.y = ball.height / 2;
ballSpeedY *= -1;
}
else if (ball.y >= stage.stageHeight-ball.height/2)
{
ball.y = stage.stageHeight - ball.height / 2;
ballSpeedY *= -1;
}
if (playerScore >= 1)
{
stage.removeEventListener(Event.ENTER_FRAME, loop);
scoreBoard_W.visible = true;
}
if (cpuScore >= 1)
{
stage.removeEventListener(Event.ENTER_FRAME, loop);
scoreBoard_L.visible = true;
}
}
Mouse.hide();
mywelcome.text = "Good Luck, " + myName;
function checkVisibility () {
for (var scoreBoard_W:String in buttonsStates) {
if(visible == true)
{
scoreBoard_W.addEventListener(MouseEvent.CLICK, goto3);
function goto3(Event:MouseEvent)
{
gotoAndStop(1,"Menu");
}
}
}
}
The problem is in this line
scoreBoard_W.addEventListener(MouseEvent.CLICK, goto3);
As you using for (var scoreBoard_W:String in buttonsStates){...} inside definition of function function checkVisibility () {...} you declare local String-type variable which block your access to button with same name.
Changing
scoreBoard_W.addEventListener(MouseEvent.CLICK, goto3);
to
this.scoreBoard_W.addEventListener(MouseEvent.CLICK, goto3);
will do the trick.

1084: Syntax error: expecting rightbrace before end of program

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);
}

Collision and movements not working as expected

I am trying to do an example of collision in Action Script 3. It's a character that should stop when it hits a platform. It works well when I move only to the right, left, up or down directions, but if I try to move in the diagonals, if the characteris colliding with the platform, the object goes to a different area of the screen.
This is the compiled example: http://dl.dropbox.com/u/5282142/GameDemo.html
And below is my code.
Now, does anyone know a better way to do what I am doing, or how can I get the character not to go to a weird position when I try to move it in a diagonal?
var level:Array = new Array();
for (var i = 0; i < numChildren; i++) {
if (getChildAt(i) is Platform) {
level.push(getChildAt(i).getBounds(this));
}
}
var speedX:int = 0;
var speedY:int = 0;
var kLeft:Boolean = false;
var kRight:Boolean = false;
var kDown:Boolean = false;
var kUp:Boolean = false;
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUpHandler);
function onKeyDownHandler(event:KeyboardEvent):void {
if (event.keyCode == 37) kLeft = true;
if (event.keyCode == 38) kUp = true;
if (event.keyCode == 39) kRight = true;
if (event.keyCode == 40) kDown = true;
}
function onKeyUpHandler(event:KeyboardEvent):void {
if (event.keyCode == 37) kLeft = false;
if (event.keyCode == 38) kUp = false;
if (event.keyCode == 39) kRight = false;
if (event.keyCode == 40) kDown = false;
}
addEventListener(Event.ENTER_FRAME, loop);
function loop(event:Event):void {
moveChar();
bound();
}
function moveChar():void {
if (kLeft) {
speedX = -10;
} else if (kRight) {
speedX = 10;
} else {
speedX *= 0.5;
}
if (kUp) {
speedY = -10;
} else if (kDown) {
speedY = 10;
} else {
speedY *= 0.5;
}
character.x += speedX;
character.y += speedY;
}
function bound():void {
if (character.x > (800 - character.width/2)){
character.x = 800 - character.width/2;
}
if (character.x < (character.width/2)){
character.x = character.width/2;
}
if (character.y > (480 - character.height/2)){
character.y = 480 - character.height/2;
}
if (character.y < (character.height/2)){
character.y = character.height/2;
}
for (i = 0; i < level.length; i++) {
if (character.getBounds(this).intersects(level[i])) {
if (speedX > 0) {
character.x = level[i].left - character.width/2;
}
if (speedX < 0) {
character.x = level[i].right + character.width/2;
}
}
}
for (i = 0; i < level.length; i++) {
if (character.getBounds(this).intersects(level[i])) {
if (speedY > 0) {
character.y = level[i].top - character.height/2;
}
if (speedY < 0) {
character.y = level[i].bottom + character.height/2;
}
}
}
}
Your Problem lies in this part of your code :
for (i = 0; i < level.length; i++) {
if (character.getBounds(this).intersects(level[i])) {
if (speedX > 0) {
character.x = level[i].left - character.width/2;
}
if (speedX < 0) {
character.x = level[i].right + character.width/2;
}
}
}
for (i = 0; i < level.length; i++) {
if (character.getBounds(this).intersects(level[i])) {
if (speedY > 0) {
character.y = level[i].top - character.height/2;
}
if (speedY < 0) {
character.y = level[i].bottom + character.height/2;
}
}
}
The character does not actually move to a random position. It is being set by you, above.
You have to create a better logic (as per the scenario) where your code should not get confused when two keys are down simultaneously & a collision occurs.