Got lost on this code and cant make it work - actionscript-3

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.

Related

2 same errors in one script

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?

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.

ES 2015 ES6 Pong can't get my ball to render correctly

Trying to build a pong game for a coding assignment with javascript 2015 or ES6 I'm unsure what I'm doing wrong as the program will not render the proper size of the ball. (It'll only render with a radii of 8 px, even if the ball is bouncing back and forth hitting nothing when radii is set to 200px) unsure why that's happening. Help would be much appreciated! ----------
import { SVG_NS } from '../settings';
export default class Ball {
constructor(radius, boardWidth, boardHeight) {
this.radius = radius;
this.boardWidth = boardWidth;
this.boardHeight = boardHeight;
this.direction = 1;
this.fill = 'red';
this.ping = new Audio('public/sounds/pong-04.wav');
this.reset();
}
wallCollision() {
const hitLeft = this.x - this.radius <= 0;
const hitRight = this.x + this.radius >= this.boardWidth;
const hitTop = this.y - this.radius <= 0;
const hitBottom = this.y + this.radius >= this.boardHeight;
if (hitLeft || hitRight) {
this.vx = -this.vx
} else if (hitTop || hitBottom) {
this.vy = -this.vy
}
}
paddleCollision(paddle1, paddle2) {
if (this.vx > 0) {
let paddle = paddle2.coordinates(paddle2.x, paddle2.y, paddle2.width, paddle2.height)
let [leftX, rightX, topY, bottomY] = paddle;
if (
this.x + this.radius >= leftX
&& this.x + this.radius <= rightX
&& this.y >= topY
&& this.y <= bottomY
) {
this.vx = -this.vx
this.ping.play()
}
} else {
let paddle = paddle1.coordinates(paddle1.x, paddle1.y, paddle1.width, paddle1.height)
let [leftX, rightX, topY, bottomY] = paddle;
if (
this.x - this.radius <= rightX
&& this.x - this.radius <= leftX
&& this.y >= topY
&& this.y <= bottomY
) {
this.vx = -this.vx
this.ping.play()
}
}
}
reset() {
this.x = this.boardWidth / 2;
this.y = this.boardHeight / 2;
this.vy = 0
while (this.vy === 0) {
this.vy = Math.floor(Math.random() * 10 - 5);
}
this.vx = this.direction * (6 - Math.abs(this.vy));
}
goal(paddle) {
paddle.score++;
this.reset();
}
render(svg, paddle1, paddle2) {
this.x += this.vx;
this.y += this.vy;
if (paddle1.score >= 3) {
this.radius = 20;
this.fill = 'skyblue';
} else if (paddle2.score >= 3) {
this.radius = 20;
this.fill = 'skyblue';
}
this.wallCollision();
this.paddleCollision(paddle1, paddle2);
let circle = document.createElementNS(SVG_NS, 'circle');
circle.setAttributeNS(null, 'r', 8);
circle.setAttributeNS(null, 'cx', this.x);
circle.setAttributeNS(null, 'cy', this.y);
circle.setAttributeNS(null, 'fill', this.fill);
svg.appendChild(circle);
const rightGoal = this.x + this.radius >= this.boardWidth;
const leftGoal = this.x - this.radius <= 0;
if (rightGoal) {
this.goal(paddle1);
this.direction = 1;
} else if (leftGoal) {
this.goal(paddle2);
this.direction = -1;
}
}
}

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