Change all platform objects to on/off [Solved]


(Lozza JP) #1

Hi everyone again, trying to make a platform tile that is either on or off and collides with the player.

My idea is that the static function is called once as a Class and changes all objects to the other state of on/off. I think I am not understanding things properly.

Basically I don’t want to have to call every objects function to change on/off I am hoping to just be able to call TogglePlatform.togglePlatform();

public class TogglePlatform extends Entity
{
	
	private var isOn:Boolean;
	
	public function TogglePlatform(x:Number, y:Number, width:int, spriteAsset:*, isOn:Boolean=true) 
	{
		this.x = x;
		this.y = y;
		this.width = width;
		this.isOn = isOn;
		setHitbox(width, 8);
	}
	
	override public function update():void
	{
		if (isOn)
		{
			if (collide("player", x + 1, y + 1) || collide("player", x - 1, y + 1))
			{
				type = "platform"; // Just make every other NPC/Enemy collide with Platform. They won't fall through when type changes.
			}
			else if (!collide("player", x, y))
			{
				type = "wall";
			}
		}
		else type = "none";
	}
	
	public static function togglePlatform():void
	{
		isOn = !isOn;
	}
}

(Justin Wolf) #2

You may want to look into the collidable property on your Entity. It looks like a simple myEntity.collidable = true; or myEntity.collidable = false would suffice! Unless of course your platforms also need to collide with other things, then the collidable sollution wouldn’t work as everything wouldn’t collide.


(Lozza JP) #3

Yea incase an NPC or enemy is walking on it they can still collide. So player can jump up through the bottom of platform and land on top.

As for an easy to change all objects on/off?


(Martí Angelats i Ribera) #4

So you basically want to be able to jump from behind and not to fall (and the other entities always collide with it), right?

In this case you should modify the way of handelling the collisions in the player.

I would do something like that.

//used variables
ySpeed:Number //The speed of the entity
yDirection:Boolean 
colliding:Vector.<Entity>
colliding2:Vector.<Entity>

//in the update function
if (ySpeed < 0)
{
	yDirection = true;
}
else if (direction)
{
	collideInto("type", x, y, colliding); //save all the entities you are in the middle
}
else
{
	if (colliding.lenght > 0)
	{
		collideInto("type", x, y, colliding2);
		for each (var e:Entity in colliding)
		{
			if (colliding2.indexOf(e) == -1)
			{
				colliding[colliding.inxedOf(e)] = null;
			}
		}
		
		for each (var e:Entity in colliding2)
		{
			if (collide.indexOf(e) == -1)
			{
				collision(e); //a function that handeles the collision
			}
		}
		
	}
	else
	{
		if (colide("type", x, y))
		{
			collision(e); //a function that handleles the collision (the same than before)
		}
	}
}

This is a general code. Probably in your specific circumstances you can make more efficient code.


(Lozza JP) #5

That’s nice and I am probably going to refactor my code now regarding collision.

My real question was changing that boolean isOn in all my objects ofTogglePlatform.

Because in the constructor parameter’s I have isOn, so some platforms in the world are on and some are off. And I want them to switch places of on/off on certain buttons etc.


(Martí Angelats i Ribera) #6

If have them stored in a Vector just do:

public function turnAllOn(item:T, index:int, vector:Vector.<T>):Boolean //Change T for the real class of your platforms
{
	item.isOn = true;
	return true; //Continue
}


//then you can simply do
vector.every(turnAllOn);

(Lozza JP) #7

I am not sure about vectors yet, but mixing this and the other post I have managed

in the platform class, some start with on, some start with off state. they have function

	public function togglePlatform():void
	{
		isOn = !isOn;
	}

In my event class

	public static function callEvent(event:int, caller:Entity):void
	{
		switch (event)
		{
			// Toggles platform class to on/off.
			case 0:
				var platformList:Array = [];
				
				caller.world.getClass(TogglePlatform, platformList);
				
				for (var i = 0; i < platformList.length; i++)
				{
					platformList[i].togglePlatform();
				}
				break;
}

Now any where in any piece of code I can just go:

Events.callEvent(0, this);

and all my platforms swap states :smiley:


(Lozza JP) #8

Just a quick aside @Copying.

Will my above array platformList constantly grow bigger every time the function is called? And as a consequence some items in the array will toggle twice, not to mention memory?

Should I clear the array after the for loop? platformList.length = 0;

EDIT: Without clearing it, it seems to behave as first described. Is this because var platformList is created every time the functino is called on case 0? Is this bad for memory?


(Mike Evmm) #9

PlatfornList seems to be a local variable (so probably no memory problems) which you are initializing (so, clearing) and repopulating every time (by calling caller.world.getClass). This probably isnt the most efficient way to go about (unless platforms are being created/destroyed), but you shouldn’t run into problems. I think.


(Lozza JP) #10

Think I should make a private var at the top and empty the array each time?


(Martí Angelats i Ribera) #11

If i were you i would do something like this:

In TogglePlatform class

public static var platforms:Vector.<TogglePlatform> = new Vector.<TogglePlatform>;

public var isOn:Boolean;

public function added():void
{
	//do whatever you want
	platforms.push(this);
}

public function removed():void
{
	var pos:int = platforms.indexOf(this);
	if (pos != -1)
	{
		platforms.splice(pos, 1);
	}
}

public static toggle(platform:TogglePlatform, index:int, vector:Vector.<TogglePlatform>):Boolean
{
	platform.isOn = !platform.isOn;
	return true;
}
public static function toggleAll()
{
	platforms.every(toggle);
}

In Events class

public static function callEvent(event:int):void
{
	switch (event)
	{
		// Toggles platform class to on/off.
		case 0:
			TogglePlatform.toggleAll();
			break;
	}
}

Be real carefull with the static variables and functions. They are powerfull but they can also create a lot of problems.

Notice that it toggles all the platforms. Even if they are not in the same World.

Edit: Forgot the existence of the splice function.


(Lozza JP) #12

Never worked with vectors :S

Cheers Copying, will play around with it.


(Mike Evmm) #13

In practice, Vectors are but arrays that only take one type of element and are (thus) more efficient