Does anyone know how to acess a function that is located in another entity


(Oli) #1

For instance i got my “collisionplatform” entity and if it collide with the enemy entity the function “playermoveblock” which is located in the player entity would be called.

»platform entity

				if (collide("Enemy", x, y)) 
			{
				playermoveblock();
			}

»player entity

	var hInput:int = 0;
				public function playermoveblock():void 
	{
		(hInput -= 1)
		
	}

My collision platform follows the player around because im tryinng to do a brawler of some kind :slight_smile:

sry for the long post but im trying to figure this out for so long now.


(Michał Rapacz) #2

Just keep the reference to the Player entity somewhere in the CollisionPlatform entity, add this line in the CollisionPlatform class:

public var player:Player;

Adding player and the platform to the world could be done like this (in your world class):

var player:Player = new Player();
var platform:CollisionPlatform = new CollisionPlatform();
add(player);
add(platform);
platform.player = player;

Then calling the player’s method from the platform entity is as simple as this:

if (collide("Enemy", x, y)) 
{
    player.playermoveblock();
}

I hope this helps a little :smile:


(Oli) #4

So I got the error fixed, but my problem now is when i actually play the game and the platform collide with the enemy my game crash, this is not a problem with the function itself because I tried to replace it by trace("") and it did the same thing…

dnt know what to do:(


(Jonathan Stoler) #5

The collide() function itself returns a matched Entity.

So…

var player:Entity;
if(player = collide("Enemy", x, y)){
	player = (player as Player);
	player.playermoveblock();
}

(Jacob Albano) #6

I think this will fail to compile actually…player is typed as an Entity so it will be accessed as a plain Entity even after you cast it. You’ll need to specify the type of that variable as Player (or Enemy, as the case may be).


(Oli) #7

well i think there is something wrong with the way the function is called because i get this error:

Error: Call to a possibly undefined method playermoveblock through a reference with static type net.flashpunk:Entity.


(Oli) #8

anyways im just gonna post my code here if anyone have time to tell me what stupid mistakes im doing :stuck_out_tongue: the function in question is called playermoveblock

THIS IS THE PLATFORM ENTITY CODE

package entities { import flash.geom.Point; import net.flashpunk.Entity; import net.flashpunk.FP; import net.flashpunk.utils.Input; import net.flashpunk.utils.Key; import entities.invisiblewall import net.flashpunk.graphics.Spritemap; import entities.Player

public class collisionplatform extends Entity
{
	
	
			
	[Embed(source = "../../assets/graphics/shadow.png")]
	private const SHADOW:Class;
	
	public var sprShadow:Spritemap = new Spritemap(SHADOW, 48, 32)
	protected const PLATFORM_HSPEED:int = 70;
	protected const PLATFORM_VSPEED:int = 50;
	protected const GRAVITY2:int = 0;
	protected const JUMP:int = 100;
	protected var a:Point;
	protected var v:Point;
	public var player:Player;

			
	
	
	public function collisionplatform()
	{
		type = "platform"
		name = "player"
		sprShadow.add("shadowstatic", [0, 1, 2, 3, 4, 5], 10, true);
		sprShadow.add("shadowjump", [6, 7, 8, 9, 10, 11], 10, true);
		setHitbox(20, 5)
		a = new Point();
		v = new Point();
		graphic = sprShadow
	}
	
	public override function update():void   
	{
		// Checking platform input.
		var hInput:int = 0;
		var vInput:int = 0;
					
					
		
		if (Input.check(Key.LEFT)) 
		{
			(hInput -= 1);
		}
		
		if (Input.check(Key.RIGHT))
		{
			(hInput += 1);
		}
		if (Input.check(Key.DOWN))
		{
			(vInput += 1);
		}
		if (Input.check(Key.UP))
		{
			(vInput -= 1);
		}
		
		if (Input.pressed(Key.SPACE)) 
		{
		sprShadow.play("shadowstatic");
		}
		sprShadow.play("shadowjump");
		
			if (collide("invisiblewall", x, y))
        {
			(vInput += 1);
		}
		
		var platform:collisionplatform = FP.world.getInstance("platform");	
		
		// Update physics.
		v.x = PLATFORM_HSPEED * hInput;
		v.y = PLATFORM_VSPEED * vInput;
		
		a.y = GRAVITY2;
		v.y += a.y;
		
		// Apply physics.
		x += v.x * FP.elapsed;
		y += v.y * FP.elapsed;
		
					
				
		//collision with ennemy
			
				var player:Entity;
				if(player = collide("Enemy", x, y)) 
			{
				player = (player as Player);
				player.playermoveblock();
			}	
				
		
		
		
		super.update();
		
	
        }
		
		

		
	
}

}

THIS IS THE PLAYER ENTITY CODE

package entities { import flash.display.BitmapData; import flash.geom.Point; import net.flashpunk.graphics.Spritemap; import net.flashpunk.Entity; import net.flashpunk.FP; import net.flashpunk.utils.Input; import net.flashpunk.utils.Key; import net.flashpunk.graphics.Anim import entities.collisionplatform

public class Player extends Entity
{
	
	[Embed(source="../../assets/graphics/sprite map low res fone.png")]
	private const KOREANDUDE:Class;
	
	[Embed(source = "../../assets/graphics/sprite map  2 low res.png")]
	private const KOREANDUDEJUMP:Class;
	
	public var angle:Number = 270.0;
	
	
	public var sprKoreanDude:Spritemap = new Spritemap(KOREANDUDE, 48, 32)
	public var sprKoreanDudeJUMP:Spritemap = new Spritemap(KOREANDUDEJUMP, 48, 32)
	protected const PLAYER_HSPEED:int = 70;
	protected const GRAVITY:int = 5;
	protected const JUMP:int = 100;
	protected var a:Point;
	protected var v:Point;
			
	
	
	
	
	public function Player()
	{
		type = "player"
		name = "player";
		sprKoreanDude.add("run", [0, 1, 2, 3, 4, 5], 10, true);
		sprKoreanDude.add("stand", [6, 7, 8, 9, 10, 11], 10, true);
		sprKoreanDudeJUMP.add("jump", [6, 7, 8, 9, 10, 11], 10, true);
		sprKoreanDudeJUMP.add("runleft", [0, 1, 2, 3, 4, 5], 10,true);
		graphic = sprKoreanDude
		//graphic = sprKoreanDudeJUMP
		setHitbox(25,30);
		a = new Point();
		v = new Point();
		//sprKoreanDude.play("stand");
	

	}
	
	
	
	public override function update():void   
	{
		// Checking player input.
		var hInput:int = 0;
		var vInput:int = 0;
		
					
		
		if (Input.check(Key.LEFT)) 
		{
			(hInput -= 1) && (graphic = sprKoreanDudeJUMP) && (sprKoreanDudeJUMP.play("runleft"));
		}
		
		if (Input.check(Key.RIGHT))
		{
			(hInput += 1) && (graphic = sprKoreanDude) && (sprKoreanDude.play("run"));

		}
		if (hInput == 0)
		{
			(graphic = sprKoreanDude) && (sprKoreanDude.play("stand"));
		}			
		
		if (Input.check(Key.SPACE)) 
		{
		(jump()) && (graphic = sprKoreanDudeJUMP) &&(sprKoreanDudeJUMP.play("jump"));
		}
		else 
		{
	    a.y = GRAVITY;
		v.y += a.y;
		}
		
		
		//UP AND DOWN MOVEMENT
			
			if (Input.check(Key.UP) && (collide("platform", x, y)))
		{
			y += -50 * FP.elapsed;
		}
		
		
			if (Input.check(Key.DOWN) && (collide("platform", x, y)))
		{
			y += 50 * FP.elapsed;
		}
					
		//CHECKED
		if (Input.check(Key.PAGE_DOWN))
		{
			playermoveblock();
		}
		

		
		// Update physics.
		v.x = PLAYER_HSPEED * hInput;
		//PLAYER_VSPEED * vInput = false
		
		a.y = GRAVITY;
		v.y += a.y;
		
		// Apply physics.
		x += v.x * FP.elapsed;
		y += v.y * FP.elapsed;
		
		//Collision with platform
		if (collide("platform", x, y))
        {
			v.y = -10;
        }
		
		
		
		
		// Simple collision with boundries.
		if(y + height > FP.screen.height)
		{
			v.y = 0;
			y = FP.screen.height - height;
		}
		
		// Update parent shit.
		super.update();
	}
	
	
						
	public function playermoveblock():void 
	{
		y += -50 * FP.elapsed;
		
		
	}
	
	protected function jump():void
	{
		if(collide("platform", x, y))
		{
			v.y = -JUMP;
		}
	}
}

}

Dont know what else to do.


(Michał Rapacz) #9

The collide function returns the collided entity - in this case “Enemy” - which is probably not what you want. Try changing the collision code to this:

//collision with ennemy
var player:Player;
if(collide("Enemy", x, y)) 
{
	player = FP.world.getInstance("player");
	player.playermoveblock();
}

and tell us if it works.


(Oli) #10

AWSOME THIS WORKS PERFECTLY:) thank you so much for the help everyone