Author Topic: collision help  (Read 1203 times)

bpercevic

  • Member
  • **
  • Posts: 87
  • Karma: +2/-0
    • View Profile
collision help
« on: Mon, Jun 7, 2010 »
hey everyone,
im having trouble dealing with collisions. ive searched the forum and didnt really find anything that helped me.  i made doors using collisions that transfer you to other states and that works fine, but when i draw objects to collide with (other than the map tiles) i run into some trouble. heres the link so you can see whats happening:

http://megaswf.com/view/35c8cad9f3dbd0e11eed56ebd892167e.html

the green are doors, they work.  the brown and red squares are what arent working right.  the brown is a stationary object and its not supposed to move upon collision, but if you move into it a few times it budges by one pixel and the old position is not re-rendered so it looks like its getting bigger.  the red is supposed to be a movable object, it actually does move with the player but the player goes halfway through it before it moves and then eventually all the way through it.  i want the brown not to move at all, and the red to move but without the player sliding through it. code below.

This is for my level class, basically i made Level extend FlxState and it just has functions for drawing the map, adding objects, and initializing camera/controls.  
Code: [Select]
package
{
import org.flixel.*;

public class Level extends FlxState
{
// map definitions : list all map layouts and tiles to use
[Embed(source="data/tiles.txt", mimeType="application/octet-stream")] public var level_1:Class;
[Embed(source="data/tiles2.txt", mimeType="application/octet-stream")] public var level_2:Class;

[Embed(source="data/tiles.png")] public var data_tiles:Class;

// pre-definitions
public var map:FlxTilemap;
public var p:Player;
public var doors:FlxGroup;

//testing
public var shapes:FlxGroup;

public function Level():void
{
// handle main
doors = new FlxGroup();
add(doors);
shapes = new FlxGroup();
add(shapes);
}

override public function update():void
{
// handle controls for shooting
p.update();
}

public function initMap(level:Class, tiles:Class):void
{
map = new FlxTilemap();
map.loadMap(new level, tiles, 8, 8);
map.collideIndex = 4;
add(map);


}

public function makeShape(X:Number=0, Y:Number=0, W:Number=1, H:Number=1, C:uint=0xFF597137):void
{
shapes.add(new Shape(X,Y,W,H,C));
}

public function addShape(shapeToAdd:Shape):void
{
shapes.add(shapeToAdd);
}

public function addDoor(doorToAdd:Shape):void
{
doors.add(doorToAdd);
}

public function addPlayer(X:Number, Y:Number):void
{
p = new Player(X,Y,0xff123cde);
add(p);
}

public function changeBG(bg:uint):void
{
FlxState.bgColor = bg;
}

public function initCamera():void
{
FlxG.follow(p);
FlxG.followBounds(0,0,map.width-8,map.height); // -8 cuz camera overlaps map by 8px
map.collide(p);
}

public function initControls():void
{
if(FlxG.keys.justPressed("F") && p.dead==false) { FlxG.flash.start(); }
}

public function checkDoor(doorNum:FlxObject):void
{
//FlxU.overlap(p, doorNum, resolve);
}

public function resolve(player:Player, door:Shape):void
{
FlxG.quake.start();
//FlxG.state = new LVL_2();
}
}
}

A few functions are overridden in the LVL_1 class like checkDoor and resolve.  This code is just level 1
Code: [Select]
package
{
import org.flixel.*;

public class LVL_1 extends Level
{
public function LVL_1(X:Number=10, Y:Number=10)
{
initMap(level_1, data_tiles);
changeBG(0xffeeeeee);
addPlayer(X,Y);
makeShape(20,80,10,10,0xffff0000);
super.create();
}

override public function update():void
{
// initialize
initControls();
initCamera();

// add objects
var firstDoor:Shape = new Shape(50,80,10,10);
addDoor(firstDoor);
var thing:Shape = new Shape(30,60,10,10,0xff736357);
addShape(thing);

shapes.collide(p);
p.collide(shapes);

super.update();

checkDoor(firstDoor);
}

override public function resolve(player:Player, door:Shape):void
{
FlxG.state = new LVL_2();
}

override public function checkDoor(door:FlxObject):void
{
FlxU.overlap(p, door, resolve);
}
}
}

so im wondering if the "shapes.collide(p)" is not meant to be used like that. (although without that player just moves right through the objects)  so i have no idea what to do.  is my code wrong?

thanks in advance this has been killing me for the past few days i just cant wrap my head around what to do.

BTM

  • Guest
Re: collision help
« Reply #1 on: Tue, Jun 8, 2010 »
Your aggind your objects in the update() function - the function gets called every frame, so in fact your adding another copy of the object every frame.

Move the code that adds the object to the constructor or beter to a create() function

bpercevic

  • Member
  • **
  • Posts: 87
  • Karma: +2/-0
    • View Profile
Re: collision help
« Reply #2 on: Tue, Jun 8, 2010 »
ohh that makes sense.  well i did change the code, and the object is pushable but only from the bottom.  if you try left/right the player goes right through it and from the top its glitchy sometimes he goes through sometimes he pushes.  heres the swf:
http://megaswf.com/view/104938430398318b8a18102ba79cf3fc.html

the new code:
Code: [Select]
public function LVL_1(X:Number=10, Y:Number=10)
{
initLevel(0xffc7b29a, bg, mg, fg, data_tiles, church, 0, 96, 120);

// add objects
shape1 = addShape(40,80,20,20,0xffff0000);
super.create();
}

override public function update():void
{
checkDoor(door1, lvl2);
shapes.collide(p);
p.collide(shapes);
super.update();
}

i wasnt sure if it should be shapes.collide(player) or p.collide(shapes) i tried them separate and both together but i get the same result.

bpercevic

  • Member
  • **
  • Posts: 87
  • Karma: +2/-0
    • View Profile
Re: collision help [SOLVED]
« Reply #3 on: Tue, Jun 8, 2010 »
ok i got it.  in the base Level class where i initiated a group for shapes, i wasnt calling shapes.update() and now collision works.