Author Topic: collide and velocitiy problem in breakout games  (Read 949 times)

ne0_komodo

  • Member
  • **
  • Posts: 30
  • Karma: +0/-0
    • View Profile
Hello there I in the process of making breakout game... very simple and basic setup.

So I have a problem,

I have this code
Code: [Select]
public override function update():void {
super.update();

FlxG.collide(mapOuterCollision, sprPlayer);
FlxG.collide(mapOuterCollision, sprBall, collideOuterCollisionBall);
}

private function collideOuterCollisionBall(map:FlxTilemap, ball:Ball):void {
ball.velocity.y *= -1;
}

The default ball velocity.y is 100.

To sum up I want make when the ball collide with wall it reverses the velocity by multiply it by -1.
But the thing is when ball collide with wall the velocity turned into 0 while I expected to be -100.

Anybody can help me why the ball.velocity.y always turned 0 when collide with the wall?

Thank you  :)

ETG

  • Active Member
  • ***
  • Posts: 115
  • Karma: +0/-0
    • View Profile
Re: collide and velocitiy problem in breakout games
« Reply #1 on: Sat, Aug 4, 2012 »
Because your calling collide, which will stop the ball against the player.
Try calling overlap.

ne0_komodo

  • Member
  • **
  • Posts: 30
  • Karma: +0/-0
    • View Profile
Re: collide and velocitiy problem in breakout games
« Reply #2 on: Sat, Aug 4, 2012 »
Because your calling collide, which will stop the ball against the player.
Try calling overlap.

I have changed the code into:
Code: [Select]
                public override function update():void {
super.update();

FlxG.collide(mapOuterCollision, sprPlayer);
FlxG.overlap(mapOuterCollision, sprBall, collideOuterCollisionBall);

if (FlxG.overlap(mapOuterCollision, sprBall)) {
trace("true");
}
}

But it always returned true in the if statement... And the ball only move 1px up after that 1px down even not yet touched the boundaries

ne0_komodo

  • Member
  • **
  • Posts: 30
  • Karma: +0/-0
    • View Profile
Re: collide and velocitiy problem in breakout games
« Reply #3 on: Sat, Aug 4, 2012 »
I have it fixed thanks
Code: [Select]
package {
import org.flixel.*;

public class PlayState extends FlxState {
[Embed(source = "../assets/usable/outer collision 32 x 32.txt", mimeType = 'application/octet-stream')]
private var arrayOuterCollision:Class;
[Embed(source = "../assets/usable/outer collision 32 x 32.png")]
private var imgOuterCollision:Class;
private var mapOuterCollision:FlxTilemap;

private var sprPlayer:Player;
private var sprPad:Pad;
private var sprBall:Ball;

private var ballVelocityX:Number = 100;
private var ballVelocityY:Number = 100;

private var grpBlock1:FlxGroup;
private var grpBlock2:FlxGroup;
private var grpBlock3:FlxGroup;
private var grpBlock4:FlxGroup;
private var grpBlock5:FlxGroup;

public function PlayState():void {
super();
}

public override function create():void {
super.create();

// Outer collision
mapOuterCollision = new FlxTilemap();
mapOuterCollision.loadMap(new arrayOuterCollision(), imgOuterCollision, 32, 32, FlxTilemap.OFF);
mapOuterCollision.x = -32;
mapOuterCollision.y = -32;
add(mapOuterCollision);

// Add player
sprPlayer = new Player(0, FlxG.height - 27, "PlayState");
add(sprPlayer);

// Add pad
sprPad = new Pad(sprPlayer.x, sprPlayer.y, sprPlayer);
add(sprPad);

// Blocks
grpBlock1 = new FlxGroup();
add(grpBlock1);
grpBlock2 = new FlxGroup();
add(grpBlock2);
grpBlock3 = new FlxGroup();
add(grpBlock3);
grpBlock4 = new FlxGroup();
add(grpBlock4);
grpBlock5 = new FlxGroup();
add(grpBlock5);
for (var i:int = 0; i < 10; i ++) {
grpBlock1.add(new Block1(0 + (i * 32), 0));
}
for (i = 0; i < 10; i ++) {
grpBlock1.add(new Block2(0 + (i * 32), 32));
}
for (i = 0; i < 10; i ++) {
grpBlock1.add(new Block3(0 + (i * 32), 64));
}
for (i = 0; i < 10; i ++) {
grpBlock1.add(new Block4(0 + (i * 32), 96));
}
for (i = 0; i < 10; i ++) {
grpBlock1.add(new Block5(0 + (i * 32), 128));
}

// Add ball
sprBall = new Ball(FlxG.width / 2, FlxG.height / 2 + 32);
add(sprBall);
}

public override function update():void {
super.update();

FlxG.collide(mapOuterCollision, sprPlayer);
FlxG.collide(mapOuterCollision, sprBall, collideOuterCollisionBall);
}

private function collideOuterCollisionBall(map:FlxTilemap, ball:Ball):void {
ballVelocityX *= -1;
ball.velocity.x = ballVelocityX;

ballVelocityY *= -1;
ball.velocity.y = ballVelocityY;
}
}
}