Skip to main content
Y2JayC
Participant
October 3, 2017
Question

Is anyone able to troubleshoot this glitch I have with my game?

  • October 3, 2017
  • 2 replies
  • 296 views

Is anyone able to troubleshoot the glitch I have when the ball hits the left side of the screen in my game. This doesn't happen on the right side so I'm not too sure why this is happening. Here is my code:

var xDirection:Number = 10;

var yDirection:Number = -10;

var targetX:Number = player_mc.x;

var easing:Number = 7;

var playerScore:Number;

function resetBallPosition():void

{

xDirection = 10;

yDirection = -10;

ball_mc.x = 2

ball_mc.y = 11

}

function checkHitLocation(player:MovieClip):void

{

var hitPercent:Number;

var ballPosition:Number = ball_mc.x - player_mc.x;

hitPercent = (ballPosition / player_mc.width);

xDirection = hitPercent * 30;

yDirection *= 1.025;

}

function initializeGame(event:MouseEvent):void

{

playerScore = 0;

showScore();

showStart();

player_mc.addEventListener(Event.ENTER_FRAME, movePlayer);

ball_mc.addEventListener(Event.ENTER_FRAME, moveBall);

bg_mc.removeEventListener(MouseEvent.CLICK, initializeGame);

}

function endGame():void

{

player_mc.removeEventListener(Event.ENTER_FRAME, movePlayer);

ball_mc.removeEventListener(Event.ENTER_FRAME, moveBall);

bg_mc.addEventListener(MouseEvent.CLICK, initializeGame);

text_mc.text = 'CLICK TO PLAY AGAIN';

text_mc.visible = true

start_mc.visible = false;

}

function moveBall(event:Event):void

{

if(ball_mc.x <= 0)

{

xDirection *= -1;

text_mc.visible = false;

start_mc.visible = false;

}

else if(ball_mc.x >= stage.stageWidth - ball_mc.width)

{

xDirection *= -1;

text_mc.visible = false;

start_mc.visible = false;

}

if(ball_mc.hitTestObject(player_mc))

{

yDirection *= -1;

ball_mc.y = player_mc.y - ball_mc.height - player_mc.height/2;

checkHitLocation(player_mc);

playerScore ++;

showScore();

text_mc.visible = false;

start_mc.visible = false;

}

if(ball_mc.y <= 0)

{

yDirection *= -1;

//resetBallPosition();

text_mc.visible = false

start_mc.visible = false;

}

else if(ball_mc.y >= stage.stageHeight - ball_mc.height)

{

endGame();

resetBallPosition();

showScore();

}

ball_mc.x += xDirection;

ball_mc.y += yDirection;

}

function movePlayer(event:Event):void

{

if(this.mouseX <= player_mc.width/2)

{

targetX = 0;

start_mc.visible = false;

}

else if(this.mouseX >= stage.stageWidth - player_mc.width/2)

{

targetX = stage.stageWidth - player_mc.width;

start_mc.visible = false;

}

else

{

targetX = this.mouseX - player_mc.width/2;

start_mc.visible = false;

}

player_mc.x += (targetX - player_mc.x) / easing;

}

function showScore():void

{

score_mc.text = "Score: " + playerScore;

start_mc.visible = false;

}

function showStart():void

{

start_mc.text = 'CLICK SCREEN TO START';

start_mc.visible = true;

}

showStart();

bg_mc.addEventListener(MouseEvent.CLICK, initializeGame);

This topic has been closed for replies.

2 replies

Ned Murphy
Legend
October 3, 2017

Use the trace command to see what the value of xDirection is when it is in the conditional at the <= 0 end of things.  It appears that it is constantly resetting between -1 and +1 when it gets there.  Maybe you can get around that by forcing it to move away from the limit when it makes the switch... as in....

if(ball_mc.x <= 0)

{

xDirection *= -1;

ball_mc.x = 1;

text_mc.visible = false;

start_mc.visible = false;

}

Colin Holgate
Inspiring
October 3, 2017

You are testing the right side of the screen with:

ball_mc.x >= stage.stageWidth - ball_mc.width

but the left side with:

if(ball_mc.x <= 0)

Try:

if(ball_mc.x <= ball_mc.width)

If either side makes the ball reflect too soon, try using ball_mc.width/2.