Help with Gravity and "collide" function (AS3 project)


(Ali Malik Wains) #1

Hello everyone I need help i am sorta stuck in here… i am not experience coder in AS3, though i am selflearner XD can you please tell me how i apply gravity to my hero and scene… how it only jumps one time (in my case… its like moving to axis x… like moving continuously if button is pressed)… so i guess i have to create variable like “OnFloor” etc etc … still i want to see how you do it… instead of wondering for hours… most of all… i am new with colliding thing… i have this “blocks” used as wall and floor… and other obstacles (using array) … i have also set “sethitBox” for both entities (“Player” and “Wall”) … so my question is… i want my player to be able to stand over obstacles and floor… and please please tell me how to put gravity…

i want to know the codes of these things: 1: Gravity 2: Colliding (making player to stand over obstacles) 3: Jump (just it jumps only once, and must be able to jump when player is over any obstacle or floor)

(i am good at understanding codes, just write it if you can’t explain… it will be a great help <3 <3 thanks)


(Kyle) #2

This is how I do those:


public class Hero extends Entity
{
	
	public var gravity:Number = 800;			
	public var jumpForce:Number = -1600;		
	
	public var velocity:Point = new Point();
	
	public var onFloor:Boolean = false;
	
	// other stuff
	// other stuff
	// other stuff
	
	override public function update():void
	{
		super.update();
		// other stuff
		
		/* HOW TO CHECK IF ON FLOOR */
		onFloor = collide("floor", x, y + 1);
		/*
		This function checks for collisions. 
		It places the hero at coordinates (x, y+1), which is one pixel below where it currently is.
		This way, if the hero is on top of a floor for example, and you move it 1 pixel down, it will collide with the floor.
		If the hero is on the air, and you move it 1 pixel down, it will not collide with any floor.
		This function returns the entity which was collided with or null if not found.
		This function DOES NOT move the hero no matter if you put x+1000 and y+200. 
		After the collision check, the hero is moved back to its original position.
		*/
		
		/* WHEN THE JUMP BUTTON IS PRESSED */
		if (onFloor) velocity.y = jumpForce;
		/*
			Check first if the hero is on a floor.
			If so, jump.
		*/
		
		/* ADDING GRAVITY */
		velocity.y += gravity * FP.elapsed;
		
		/* MOVING THE HERO */
		moveBy(velocity.x * FP.elapsed, velocity.y * FP.elapsed, ["floor", "wall"]);
		/*
		This function moves the hero by the specified amount in both x and y axes. 
		You can also pass in an array of the types of entities you want it to collide with.
		This way, it will move the hero but stop when it collides with an entity of type "floor" or "wall" as in this example.
		
		All entities have a variable called type. You can set it inside an entity's constructor:
			type = "floor";
		
		When the hero collides with the said entity, the moveCollide functions gets called. This will be discussed below.
		*/
	}
	
	override public functions moveCollideY(entity:Entity):Boolean
	{
		velocity.y = 0;
		/*
		When the above moveBy() function detects a collision with "floor" or "wall" along the Y axis, this function gets called.
		The entity passed is the entity which it collided with.
		
		You want to set the velocity.y to 0 because when you are on the floor, you stop but your velocity would still increase due to gravity.
		If your hero walks towards an edge and falls, his velocity would be so big he would just zap down so fast.
		So set this to 0.
		*/
		
		return true;
		/*
		This statement just tells the engine if we should collide with the object or not.
		
		If true, the normal collision thing you imagined would happen.
		If false, the hero will pass through the floor but you will still get notified if the hero overlaps the floor.
		So set this to true.
		*/
	}
	
	override public functions moveCollideX(entity:Entity):Boolean
	{
		velocity.x = 0;
		/*
		Same as above.
		*/
		
		return true;
		/*
		Same as above.
		*/
	}
}




(Ali Malik Wains) #3

Thank you so much SacrificerXY, though as i said… i am new… but thank you so much… i learned it for future… there are few questions if you can answer me…

public var velocity:Point = new Point();

velocity.y += gravity * FP.elapsed;

we haven’t declared var velocity as this.y will it still move? if we declared to velocity:Point = new Point();


#4

Ali,

Sorry for asking but, why would you use Point as the type of a velocity variable?

Also, the player will move because SacrificerXY is using the MOVEBY function, which pretty much helps/solves all the math for the movement part, including collisions if you set the parameter.


(Kyle) #5

Those codes are just calculations. The real magic happens in the moveBy function:

moveBy(thisMuchHorizontally, thisMuchVertically, ["typesYouWantToCollideWith"]);

In my case, velocity.y is how much I want the hero to move vertically while this.y pertains to the actual position of the hero.

For as long as you supply a number other than zero to thisMuchHorizontally or thisMuchVertically, the hero should move. No need to touch this.y.


(Zachary Lewis) #6

Point is a pretty sweet class, because you can do lots of stuff pretty easily with it. Want to have a Boost button that shoots the player in the direction he’s traveling?

// Apply the boost.
velocity.normalize(boostStrength);

What about tiles that fling the player when he touches it?

// Apply a fling force.
velocity.add(flingForce);

Lots of cool stuff you can do easily with a Point.


#7

@zachwlewis

Thanks a lot for your explanation, I believe that now I see some very good advantages of using this framework rather than Flixel, even if I really like working with both of them.


(Ali Malik Wains) #8

Now thats awesome @zachwlewis, @Macronaut actually Point worked very well