No keyboard input AS3 - actionscript-3

So I'm trying to get keyboard input from the user to move a character. It works in another program that I was using, and I copy pasted, but it isn't working in this one. It gives me Line 87, Column 38 1119: Access of possibly undefined property EVENT_FRAME through a reference with static type Class. I can't seem to figure out what the issue is.
This is the buttonClick function that is used to when I hit the start button.
public function buttonClick(ev:MouseEvent):void
{
createGameScreen();
this.mcLink.gotoAndPlay("Idle");
this.mcLink.x=50;
this.mcLink.y=200;
this.mcLink.scaleX=this.mcLink.scaleY=3;
this.stage.addEventListener(Event.EVENT_FRAME, this.enterFrameHandler, false, 0, true);
}
This is the event handler function for the keyboard input.
public function enterFrameHandler($e:Event):void
{
if (this.mcLink)
{
if (KeyboardManager.instance.isKeyDown(KeyCode.DOWN))
{
if (this.mcLink.y + this.mcLink.height > this.stage.stageHeight || this.mcLink.y - this.mcLink.height <= 0)
{
this.mcLink.y += -15;
mcLink.gotoAndPlay("Idle");
return;
}
this.mcLink.y += _nHeroMovementSpeed;
mcLink.gotoAndPlay("Down");
}
else if (KeyboardManager.instance.isKeyDown(KeyCode.UP))
{
if (this.mcLink.y + this.mcLink.height > this.stage.stageHeight || this.mcLink.y - this.mcLink.height <= 0)
{
this.mcLink.y += 15;
mcLink.gotoAndPlay("Idle");
return;
}
this.mcLink.y -= _nHeroMovementSpeed;
mcLink.gotoAndPlay("Up");
}
if (KeyboardManager.instance.isKeyDown(KeyCode.LEFT))
{
if (this.mcLink.x + this.mcLink.width > this.stage.stageWidth || this.mcLink.x - this.mcLink.width <= 0)
{
this.mcLink.x += 15;
mcLink.gotoAndPlay("Idle");
return;
}
this.mcLink.x -= _nHeroMovementSpeed;
mcLink.gotoAndPlay("Left");
}
else if (KeyboardManager.instance.isKeyDown(KeyCode.RIGHT))
{
if (this.mcLink.x + this.mcLink.width > this.stage.stageWidth || this.mcLink.x - this.mcLink.width <= 0)
{
this.mcLink.x += -15;
mcLink.gotoAndPlay("Idle");
return;
}
this.mcLink.x += _nHeroMovementSpeed;
mcLink.gotoAndPlay("Right");
}
}
}

Did you mean Event.ENTER_FRAME?
this.stage.addEventListener(Event.ENTER_FRAME, this.enterFrameHandler, false, 0, true);
// ^^^^^^^^^^^ EVENT_FRAME isn't a known Event.

Related

Error 1136 : incorrect number of arguments. Expected 0

can anybody tell me what is wrong with this code ...How to solve this code i use this code make monster move to the checkpoints...but all of the check points get some warning from Line 22-30 1136: Incorrect number of arguments. Expected 0.
package Game
{
import flash.display.MovieClip;
import flash.events.*;
import flash.geom.*;
public class Monster extends MovieClip
{
public var currLife:Number;
private var maxLife, gold, speed, currIndex, slowTimer:Number;
private var checkPoints:Array;
public function Monster()
{
maxLife = C.MONSTER_LIFE;
currLife = maxLife;
speed = C.MONSTER_SPEED;
currIndex = 0;
checkPoints = new Array();
checkPoints.push(new Point(85,140));
checkPoints.push(new Point(85,320));
checkPoints.push(new Point(325,320));
checkPoints.push(new Point(325,200));
checkPoints.push(new Point(265,200));
checkPoints.push(new Point(265,80));
checkPoints.push(new Point(505,80));
checkPoints.push(new Point(505,380));
checkPoints.push(new Point(630,380));
}
public function update()
{
var finalSpeed:Number;
if (slowTimer > 0)
{
finalSpeed = speed / 2;
slowTimer--;
}
else
finalSpeed = speed;
if (currIndex < checkPoints.length)
{
//move in the direction of the checkpoint
if (this.x < checkPoints[currIndex].x)
this.x += Math.min(finalSpeed, Math.abs(this.x -
checkPoints[currIndex].x));
else if (this.x > checkPoints[currIndex].x)
this.x -= Math.min(finalSpeed, Math.abs(this.x -
checkPoints[currIndex].x));
if (this.y < checkPoints[currIndex].y)
this.y += Math.min(finalSpeed, Math.abs(this.y -
checkPoints[currIndex].y));
else if (this.y > checkPoints[currIndex].y)
this.y -= Math.min(finalSpeed, Math.abs(this.y -
checkPoints[currIndex].y));
if ((this.x == checkPoints[currIndex].x) &&
(this.y == checkPoints[currIndex].y))
{
currIndex += 1;
}
}
//display
if (currLife > 0)
mcLifeBar.width = Math.floor((currLife/maxLife)*
C.LIFEBAR_MAX_WIDTH);
else
mcLifeBar.width = 0;
}
public function takeDamage(amtDamage)
{
if (this.currLife <= 0)
return;
this.currLife -= amtDamage;
if (this.currLife <= 0)
{
this.gotoAndPlay("death");
}
}
public function slowDown(amt)
{
slowTimer = amt;
}
public function hasReachedDestination()
{
return (this.currIndex == checkPoints.length);
}
}
}
Please help me to solve this problem ...i'm just a noob in AS3
i use that code in above for GameController.as
and this is update for GameController
public function update(evt:Event)
{
//Update the mobs
if ((currWave < maxWave) && (monsters.length == 0))
{
currWave++;
//spawn the monsters
spawnWave(currWave);
}
for (i=monsters.length - 1; i >= 0; i--)
{
if (monsters[i].currLife > 0)
{
monsters[i].update();
}
//Check if monster reaches the end of their path
if (monsters[i].hasReachedDestination())
{
monsters[i].gotoAndStop("remove");
life -= 1;
currGold -= C.MONSTER_GOLD;
}
if (monsters[i].currentLabel == "remove")
{
mcGameStage.removeChild(monsters[i]);
monsters.splice(i,1);
//Award Gold
currGold += C.MONSTER_GOLD;
}
}
//Update all the towers
for (i in towers)
{
towers[i].update();
}
//Update all the bullets
for (i=bullets.length - 1; i >= 0; i--)
{
bullets[i].update();
if (bullets[i].currentLabel == "remove")
{
mcGameStage.removeChild(bullets[i]);
bullets.splice(i,1);
}
}
//******************
//Handle Display
//******************
//Display new Score
mcGameUI.txtLife.text = String(life);
mcGameUI.txtGold.text = String(currGold);
mcGameUI.txtWave.text = String(currWave) + " / " + String(maxWave);
//Check for end game
if (life <= 0)
{
gameOver();
//stop all subsequent code in this update to run
return;
}
else if ((currWave == maxWave) && (monsters.length == 0))
{
gameWin();
}
}
private function spawnMonster(xPos, yPos)
{
var monsterToSpawn = new Monster();
monsterToSpawn.x = xPos;
monsterToSpawn.y = yPos;
monsters.push(monsterToSpawn);
mcGameStage.addChild(monsterToSpawn);
}
private function spawnWave(currWave)
{
if (currWave == 1)
{
spawnMonster(C.MONSTER_START_X, C.MONSTER_START_Y);
}
else if (currWave == 2)
{
for (i = 0; i < 2; i++)
{
spawnMonster(C.MONSTER_START_X - 40*i, C.MONSTER_START_Y);
}
}
}
Hi I don't understand why you are getting this error, but this can help you,
try the follow code instead of Point();
var checkPoints:Array=new Array({x:85,y:140},
{x:85,y:320},
{x:325,y:320},
{x:325,y:200},
{x:265,y:200},
{x:265,y:80},
{x:505,y:80},
{x:505,y:380},
{x:630,y:380}
);

Platformer game hit test

Ok, so I have an object on the stage that moves on the "world" movieclip. I'm trying to make it so that when you're moving right. If the movieclip inside the moving movieclip("dude") called hitD collides with the walls in world, the dude stops moving forward.
Screen shots if it might help.
General stage dude object selected
http://prntscr.com/5bgjfq the world is everything but the ball
http://prntscr.com/5bgjuh
hitD
If anyone has any way they can modify these collision physics since my current code is sketchy as hell, all suggestions and ideas are welcome.
var started:Boolean;
const NUMLEVELS = 3;
var status:String;
stage.focus = stage;
if (! started)
{// Only ever do this once!
status = "falling";
started = true;
var speedX:Number = 5;
var speedY:Number = 0;
var topSpeedY:Number = 50;
var start_x:Number = dude.x;
var start_y:Number = dude.y;
var keysDown:Object = new Object();
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
stage.addEventListener( Event.DEACTIVATE, appDeactivate );
dude.addEventListener(Event.ENTER_FRAME, moveDude);
var W:Number = 15;
var snows:Array = new Array();
}
for (var b:int = 0; b < 50; b++)
{
var snow:Snow = new Snow();
snows.push(snow);
addChild(snow);
}
function cleanup()
{
stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
stage.removeEventListener(KeyboardEvent.KEY_UP, keyReleased);
stage.removeEventListener( Event.DEACTIVATE, appDeactivate );
dude.removeEventListener(Event.ENTER_FRAME, moveDude);
}
function keyIsDown(key:uint):Boolean
{
return Boolean(key in keysDown);
}
function keyPressed(e:KeyboardEvent):void
{
keysDown[e.keyCode] = true;
}
function keyReleased(e:KeyboardEvent):void
{
delete keysDown[e.keyCode];
}
function appDeactivate( event:Event ):void
{
// Get rid of all keypress info when app loses focus
keysDown=new Object();
}
function moveDude(e:Event):void
{
var obj:Object = e.target; //setting dude as object
// for now, if you get off the top of the screen you win
if (obj.y < 0)
{
cleanup();
nextScene();
return;
}
// if character dies, restart
if (obj.y > stage.stageHeight + 100)
{
gotoAndStop(1);
obj.x = start_x;
obj.y = start_y;
}
if (death!=null)
{
if (obj.hitTestObject(death))
{
trace("Dead");
}
}
if (status=="falling")
{
speedY++;
if (speedY>topSpeedY)
{
speedY = topSpeedY;
}
for (i = 0; i<2*speedY; i++)
{
obj.y++;
if (world.hitTestPoint(obj.x - obj.width / 2,obj.y,true) || world.hitTestPoint(obj.x + obj.width / 2,obj.y,true))
{
status = "ground";
break;
}
}
}
else if (status == "jumping")
{
speedY--;
for (i = 0; i<2*speedY; i++)
{
obj.y--;
if (world.hitTestPoint(obj.x - obj.width / 2,obj.y - obj.height,true) || world.hitTestPoint(obj.x + obj.width / 2,obj.y - obj.height,true))
{
speedY = 0;
break;
}
}
if (speedY==0)
{
status = "falling";
}
}
else if (status == "ground")
{
if (! world.hitTestPoint(obj.x - 8,obj.y,true) && ! world.hitTestPoint(obj.x + 8,obj.y + 4,true))
{
speedY = 0;
status = "falling";
}
if (keyIsDown(Keyboard.UP))
{
status = "jumping";
speedY = 10;
}
}
if (keyIsDown(Keyboard.DOWN)&&status=="ground")
{
dude.gotoAndStop("duck");
}
else
{
if (keyIsDown(Keyboard.SHIFT))
{
speedX = 10;
}
else
{
speedX = 5;
}
if (keyIsDown(Keyboard.LEFT))
{
for (i = 0; i<speedX; i++)
{
obj.x--;
dude.ball.rotation--; //dude.ball is a movieclip similar to dude.hitD, it spins when you move.
if (world.hitTestPoint(obj.x - obj.width / 2 + 4,obj.y - 8,true) || world.hitTestPoint(obj.x - obj.width / 2,obj.y - obj.height + 8,true))
{
dude.ball.rotation++;
obj.x++;
break;
}
}
}
else if (keyIsDown(Keyboard.RIGHT))
{
//dude.gotoAndStop("right");
//obj.scaleX = 1;
for (i = 0; i<speedX; i++)
{
obj.x++;
dude.ball.rotation++;
// The number in obj.y-4 affects the climbing ability
if (status == "ground")
{
//dude.height+= 0.1;
//dude.width += 0.1;
}//so here I'm checking if it hits the lower corner or top right corner or hitD
if (world.hitTestPoint(dude.hitD.x + obj.hitD.width/2 , obj.hitD.y,true) || world.hitTestPoint(obj.hitD.x + obj.hitD.width/2,obj.hitD.y - obj.hitD.height ,true))
//if (world.hitTestObject(obj))
{
dude.ball.rotation--;
obj.x--;
break;
}
}
}
dude.gotoAndStop(1);
}
while (status == "ground" && (world.hitTestPoint(obj.x-8, obj.y-1, true) || world.hitTestPoint(obj.x+8, obj.y-1, true)))
{
obj.y--;
}
const BORDER = 50;
var diff:int;
// Check right border:
diff = obj.x + BORDER - stage.stageWidth;
if (diff>0 && world.x>=stage.stageWidth-world.width)
{
obj.x -= diff;
world.x -= diff;
background1.x -= diff;
if (death != null)
{
death.x -= diff;
}
}
// Check left border:
diff = obj.x - BORDER;
if (diff<0 && world.x<=0)
{
obj.x -= diff;
world.x -= diff;
background1.x -= diff;
if (death != null)
{
death.x -= diff;
}
}
// Check bottom border:
diff = obj.y + BORDER - stage.stageHeight;
if (diff>0)
{
obj.y -= diff;
world.y -= diff;
background1.y -= diff;
if (death != null)
{
death.y -= diff;
}
}
// Check top border:
diff = obj.y - BORDER;
if (diff<0)
{
obj.y -= diff;
world.y -= diff;
background1.y -= diff;
if (death != null)
{
death.y -= diff;
}
}
if (obj.x > stage.stageWidth - 25)
{
if (currentFrame<NUMLEVELS)
{
gotoAndStop(currentFrame+1);
obj.x = 25;
}
else
{
obj.x = stage.stageWidth - 25;
}
}
else if (obj.x<25)
{
if (currentFrame>1)
{
gotoAndStop(currentFrame-1);
obj.x = stage.stageWidth - 25;
}
else
{
obj.x = 25;
}
}
}
Thanks in advance for any help you can provide :)
For player physics, it's more clear approach to make a central ENTER_FRAME handler function that just for calculating the transformations to be applied to player.
You can still get information from out, but you just process the final output there. Else, it could be problematic, especially when things gets more complex and when you want to add some more feature.(e.g. Imagine in the future, you wanted to add fans on the ground that blows air up, and player have to rise when on them. There you have a lot to change, and probably with many problems.
For collision detection, this function will provide you this information: to which direction(s) the player can't go with variables 'UpColl', 'DownColl', 'Right Coll' and 'LeftColl'.
Then you can refer to this information from your main function that applies the transformation to your player. I'll give example to that also below.
function collEveryFrame(event:Event):void
{
// capture player positions
player_x = WORLD.player.x;
player_y = WORLD.player.y;
// the movie clip object where you store your solid objects. Remember, ALL MovieClip objets inside this MovieClip will taken as solid objects in your game, and regardless their shape, their boundingBox will be taken as collison. So they will all be square.
collContainer = WORLD.cW;
// your player's collision object
playerColl = WORLD.player;
// RIGHT SQUARE COLLISION DETECTION
for (var i:int; i < collContainer.numChildren; i++)
{
// Check if any collision object colliding with player
if (playerColl.hitTestObject(collContainer.getChildAt(i)))
{
// One collision detected. Check 'from which side' does the player colliding with the object;
if (collContainer.getChildAt(i).y > playerColl.y + playerColl.height - p1MoveSpeed)
{
playerColl.y = collContainer.getChildAt(i).y - playerColl.height;
DownColl = true;
}
else if (collContainer.getChildAt(i).y + collContainer.getChildAt(i).height < playerColl.y + p1MoveSpeed)
{
playerColl.y = collContainer.getChildAt(i).y + collContainer.getChildAt(i).height;
UpColl = true;
}
else if (collContainer.getChildAt(i).x + collContainer.getChildAt(i).width < playerColl.x + p1MoveSpeed)
{
playerColl.x = + collContainer.getChildAt(i).x + collContainer.getChildAt(i).width;
LeftColl = true;
}
else if (collContainer.getChildAt(i).x > playerColl.x + playerColl.width - p1MoveSpeed)
{
playerColl.x = + collContainer.getChildAt(i).x - playerColl.width;
RightColl = true;
}
}
}
// RIGHT SQUARE COLLISION DETECTION [End]
}
Transformation function;
function playerMovement(event:Event):void
{
// (apply this for all sides)
// if nothing keeps player from going right;
if (! RightColl)
{
// Apply everything currently pushing the player to right
if (keyIsDown(Keyboard.RIGHT))
{
movement_Right = 15;
}else{
movement_Right = 0;
}
// example fictional wind function returns wind speed
windSpeed = getWindSpeed();
player.x += movement_Right + windSpeed + etc + etc;
// say windSpeed is -5 (Wind is coming from right, so pushing the player to left)
// so if user pressing right arrow key, player will move to right 10px for every frame, else 5px to left, etc.
}
}
In this way, everything about physics will be easy to implement.
For example, when calculating the movement to down, add gravity and jump etc.

TypeError: Error #1009 cant find the NULL object

I got this really annoying error
"TypeError: Error #1009: Cannot access a property or method of a null object reference.
at monke2_fla::MainTimeline/update2()"
I can't find the NULL var, any help would be great as the script works fine, apart from the #1009 error.
var randomX:Number = Math.random() * 800;
Banana_mc1.x = randomX;
Banana_mc1.y = 0;
var speed:Number = 10;
var speed2:Number = 5;
var speed3:Number = 2;
var speed4:Number = 6;
Banana_mc1.addEventListener(Event.ENTER_FRAME, moveDown);
var playerScore:int = 0;
var stopLoop = 0;
if (stopLoop == 1) { } else {
function moveDown(e:Event):void
{
Banana_mc1.y += speed;
Banana_mc2.y += speed2;
Banana_mc3.y += speed3;
Banana_mc4.y += speed3;
Banana_mc5.y += speed;
Banana_mc6.y += speed4;
Snake_mc1.y += speed2;
Bunch.y += speed3;
Snake_mc2.y += speed3;
}
}
// KEYS
stage.addEventListener (KeyboardEvent.KEY_DOWN, myFunction) ;
//Monkey.addEventListener(Event.ENTER_FRAME, update);
function myFunction (event: KeyboardEvent)
{
if(event.keyCode == Keyboard.LEFT)
Monkey.x -= 10;
if(event.keyCode == Keyboard.RIGHT)
Monkey.x += 10;
}
var hitAry:Array = [Banana_mc1,Banana_mc2,Banana_mc3,Banana_mc4,Banana_mc5,Banana_mc6];
var hitAry2:Array = [Snake_mc1,Snake_mc2];
stage.addEventListener(Event.ENTER_FRAME, update2);
function update2(e:Event):void
{
var hitAry:Array = [Banana_mc1,Banana_mc2,Banana_mc3,Banana_mc4,Banana_mc5,Banana_mc6];
var hitAry2:Array = [Snake_mc1,Snake_mc2];
for (var i:int=0; i < hitAry.length; i++) {
if (Monkey.hitTestObject(hitAry[i])) {
trace("HIT");
hitAry[i].parent.removeChild(hitAry[i]);
trace(hitAry[i]);
playerScore+=1;
playerScoreText.text = ("" + playerScore);
} else {
trace("MISS"); }
if (Floor.hitTestObject(hitAry[i])) {
trace("HIT The Floor");
hitAry[i].gotoAndPlay(35);
stopLoop = 1;
} else {
trace("MISS"); }
}
for (var b:int=0; b < hitAry2.length; b++) {
if (Monkey.hitTestObject(hitAry2[b])) {
Monkey.Head.gotoAndPlay(23);
trace("HIT");
hitAry2[b].parent.removeChild(hitAry2[b]);
playerScore-=1;
if (playerScore <= 0) { playerScore = 0; }
playerScoreText.text = ("" + playerScore);
} else {
trace("MISS"); }
}
if (Monkey.hitTestObject(allleft)) {
Monkey.Head.gotoAndPlay(23);
trace("BITE");
playerScore-=100;
if (playerScore <= 0) { playerScore = 0; }
playerScoreText.text = ("" + playerScore);
} else {
trace("NO BITE"); }
if (Monkey.hitTestObject(allright)) {
Monkey.Head.gotoAndPlay(23);
trace("BITE");
playerScore-=100;
if (playerScore <= 0) { playerScore = 0; }
playerScoreText.text = ("" + playerScore);
} else {
trace("NO BITE"); }
if (Monkey.hitTestObject(Bunch)) {
trace("HIT BUNCH");
Bunch.parent.removeChild(Bunch);
playerScore+=10;
playerScoreText.text = ("" + playerScore);
}
}
This is for an assignment that is over due and I am hitting a brick wall, Thanks in advance
Chelsea
Well, near the bottom of your code you say: Bunch.parent.removeChild(Bunch);
But you also have: Banana_mc1.addEventListener(Event.ENTER_FRAME, moveDown); which invokes a function that keeps referring to Bunch.
You could test that by NOT removing Bunch. If that fixes the null var problem then you'll have to decide how to proceed. Do you want the game to continue or end after Bunch goes away?
Then there's also this:
hitAry[i].parent.removeChild(hitAry[i]);
trace(hitAry[i]);
You're trying to trace it right after you remove it! That mightmake hitAry[i] null!

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.

Error with jump function when putting script on movieclip

I'm having a problem with my jump function for a platformer engine. I've moved all of the hit testing onto the individual platforms in the level, so I don't have to write things out for each individual one. The problem with this is that only the first movieclip added to the stage can be jumped on to. The others just make the player sink through the floor if up is held down (as they should- as he's not meant to be there there's nothing to stop him).
So the problem is that the check for the jump just isn't registering properly on the second block. Here's the code (sorry about the excessive MovieClip(parent) s)
var playerRef:MovieClip = MovieClip(parent).player;
stage.addEventListener(Event.ENTER_FRAME, hitUpdate);
function hitUpdate(e:Event):void {
if (playerRef.hitTestPoint(playerRef.x,this.y - (playerRef.height/2)) && playerRef.x + playerRef.width > this.x && playerRef.x < this.x + this.width && !MovieClip(parent).upDown) {
MovieClip(parent).downMomentum = 0;
playerRef.y = this.y - playerRef.height;
}
if (playerRef.hitTestPoint(playerRef.x,this.y + this.height) && playerRef.x + playerRef.width > this.x && playerRef.x < this.x + this.width) {
MovieClip(parent).downMomentum = 0;
playerRef.y = this.y + this.height + MovieClip(parent).gravity;
}
if (playerRef.hitTestPoint(this.x,playerRef.y) && playerRef.y + playerRef.height > this.y && playerRef.y < this.y + this.height) {
MovieClip(parent).speed = 0;
playerRef.x = this.x - playerRef.width;
}
if (playerRef.hitTestPoint(this.x + this.width,playerRef.y) && playerRef.y + playerRef.height > this.y && playerRef.y < this.y + this.height) {
MovieClip(parent).speed = 0;
playerRef.x = this.x + this.width;
}
if (playerRef.hitTestObject(this)) {
MovieClip(parent).groundTouch = true;
} else {
MovieClip(parent).groundTouch = false;
}
}
And the jump function runs like this:
stage.addEventListener(Event.ENTER_FRAME, updates);
function updates(e:Event):void{
player.y += downMomentum;
if(!groundTouch && downMomentum < downMomCap){
downMomentum += gravity;
}
if(downMomentum > downMomCap){
downMomentum = downMomCap;
}
if(upDown && !jumping && groundTouch){
jumping = true;
jumpTimer.reset();
jumpTimer.start();
}
}
jumpTimer.addEventListener(TimerEvent.TIMER, jumpTick);
function jumpTick(e:TimerEvent):void{
if(downMomentum*-1 < downMomCap){
downMomentum -= jumpProg * jumpIncrement;
}else{
downMomentum = downMomCap * -1;
}
jumpProg -= 1;
}
jumpTimer.addEventListener(TimerEvent.TIMER_COMPLETE, jumpDone);
function jumpDone(e:TimerEvent):void{
jumpTimer.stop();
jumping = false;
jumpProg = 5;
}
I just can't find a solution to this, though I suspect it's something to do with how it checks for the player touching the platform.