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.
Related
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.
I know there are alot of posts about this error but im new at AS3 and i can't figure out how to use any of these specific answers to help me.
Im working on a project at school and I keep getting
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at pong_fla::MainTimeline/loop()
I have tried alot of things to try and fix this but it still keeps occuring.
Here is the code with the loop its referring to.
var ballSpeedX: int = -6;
var ballSpeedY: int = -6;
var cpuPaddleSpeed: int = 3;
var playerScore: int = 0;
var cpuScore: int = 0;
var wintotal: int = 1;
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 (playerScore == wintotal) {
gotoAndStop(3);
}
if (cpuScore == wintotal) {
gotoAndStop(4);
}
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;
}
}
I am new to StackOverflow, if i can improve on my question plaese let me know.
Here is the full file:
DropBox Link
it happens because MovieClips don't exist in other frames
removing the listener fixed most of the errors (not all of them, however)
if (playerScore == wintotal) {
//remove the listener when leaving the frame
stage.removeEventListener(Event.ENTER_FRAME, loop);
gotoAndStop(3);
}
if (cpuScore == wintotal) {
//remove the listener when leaving the frame
stage.removeEventListener(Event.ENTER_FRAME, loop);
gotoAndStop(4);
}
//check if MovieClips exist
if(!playerPaddle || !cpuPaddle){
return;
}
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at pong_fla::MainTimeline/loop()
actually this error says what to do. Flash cant find a MC on your stage or MC dont have that property to use ;) Just check your code again and use trace for detect what is cause your error.
This error is thrown when ActionScript can't find an object reference at the current scope. I would double check that both playerPaddle and ball exist on the stage and not inside MovieClips. Remember instance names are case sensitive so Ball wouldn't be the same as ball.
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.
I'm programming a ball game in Adobe Flash, Javascript 3 and I getting a error at line 113( the last line ) that says:
1087: Syntax error: extra characters found after end of program.
package
{
import flash.display.MovieClip
import flash.text.TextField
import flash.events.Event
import flash.events.MouseEvent
public class DocumentMain extends MovieClip
{
public const GRAVITY:Number = 2;
public const BOUNCE_FACTOR:Number = 0.8;
public var _bounces:TextField;
public var _highscore:TextField;
public var _ball:Ball;
private var _vx:Number;
private var _vy:Number;
public function DocumentMain():void
{
_vx = 10;
_vy = 0;
_ball.buttonMode = true;
addEventListener(Event.ENTER_FRAME, enterFrameHandler);
addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
}
private function enterFrameHandler(e:Event):void
{
// gravitate the ball
_vy += GRAVITY;
// move the ball
_ball.x += _vx;
_ball.y += _vy;
// check boundaries for collusion
checkBoundaryCollisions();
}
private function mouseDownHandler(e:MouseEvent):void
{
//hit the ball if it has been clicked
if (e.target == _ball)
{
hit(e.target.mouseX, e.target.mouseY);
}
}
private function checkBoundaryCollisions():void
{
var left:Number;
var right:Number;
var bottom:Number;
var top:Number;
left = _ball.x - (_ball.width / 2);
right = _ball.x + (_ball.width / 2);
bottom = _ball.y + (_ball.height / 2);
top = _ball.y - (_ball.height / 2);
if (left < 0 && _vx < 0)
{
_ball.x = _ball.width / 2;
_vx *= -1;
}
else if (right > stage.stageWidth && _vx > 0)
{
_ball.x = stage.stageWidth - (_ball.width / 2);
_vx *= -1;
}
if (top < 0 && _vy < 0)
{
_ball.y = _ball.height / 2;
_vy *= -1;
}
else if (bottom > stage.stageHeight && _vy > 0)
{
_ball.y = stage.stageHeight - (_ball.height / 2);
_vy *= -BOUNCE_FACTOR;
_vx *= BOUNCE_FACTOR;
if (Number(_bounces.text) > Number(_highscore.text))
{
_highscore.text = _bounce.text;
}
_bounces.text = "0";
}
}
private function hit(hitX:Number, hitY:Number):void
{
//increment bounces
_bounces.text = String.(Number(_bounces.text) + 1);
//adjust the vertical velocity of the ball
if (_vy > 0)
{
_vy *= -BOUNCE_FACTOR / 2 ;
}
_vy -= HIT_FORCE;
//adjust the horizontaly velocity of the ball
if (_vx * hitX > 0)
{
_vx *= -BOUNCE_FACTOR;
}
_vx -= ( 2 * hitX / _ball.width) * HIT_FORCE;
}
}
}
}
get rid of the last curly brace }. You have too many at the end of the file.
I also think you mean Actionscript 3.
I am a beginner of AS3. I want to create a simple bouncing ball in a space limited in the canvas of 400*450. But it cannot work when I publish it. Anyone can help me figure this out?
PS: Is there any nice website can help me learn more things about AS3?
Here is my code:
function ballmoving(evt:Event = null):void
{
var vel_x = 5;
var vel_y = 6;
ball.x = -20;
ball.y = 280;
ball.x += vel_x;
ball.y += vel_y;
if (ball.x > stage.stageWidth - ball.width / 2 || ball.x < 0 + ball.width /2)
{
vel_x *= -1;
}
else if (ball.y > 280 || ball.y < 0 + ball.height /2)
{
vel_y *= -1;
}
}
ballmoving();
RecycleButton.addEventListener(MouseEvent.CLICK, reset);
function reset(event:MouseEvent):void
{
ball.x = -20;
ball.y = 280;
ballmoving();
}
Try something like :
var vel_x = 5;
var vel_y = 6;
function ballmoving(evt:Event = null):void
{
ball.x += vel_x;
ball.y += vel_y;
if (ball.x > stage.stageWidth - ball.width / 2 || ball.x < ball.width /2)
{
vel_x *= -1;
}
if (ball.y > 280 || ball.y < ball.height /2)
{
vel_y *= -1;
}
}
stage.addEventListener(Event.ENTER_FRAME, ballmoving);
RecycleButton.addEventListener(MouseEvent.CLICK, reset);
function reset(event:MouseEvent):void
{
ball.x = -20;
ball.y = 280;
}