Entity1 adds Entity2 to Entity1's current location


(Gil) #1

In my project I have made a coin entity, and I’d like that whenever an “enemy” entity is destroyed, the coin is dropped when the enemy’s “destroy” function is called (and the “enemy” is removed from the world).

I successfully added the coin to the GameWorld pending the enemy’s “destroy” function, but despite plenty attempts I can’t add the coin to the enemy’s last location. By default the coin is added to (0,0) and I can’t change that.

I’d like the coin to be added to the enemy’s last location to make it look like he “dropped” it upon death. Please help!


(Jacob Albano) #2

Can you show the code where you add the coin entity? Also, is there any place in your Coin class where you set its position? It’s possible you’re overwriting values by accident.


(Gil) #3

The coin class is pretty simple, i’m just using it to test at the moment, code here:

package entities 
{
	import net.flashpunk.Entity;
	import net.flashpunk.graphics.Spritemap;
	/**
	 * ...
	 * @author Gil
	 */
	public class Coin extends Entity
	{
		[Embed(source = "../../assets/gfx/Coin.png")]
		public var ART_COIN:Class;
		public var coinSprite:Spritemap = new Spritemap(ART_COIN, 16, 16);
		
		public function Coin() 
		{
			coinSprite.add("Spin", [0, 1, 2, 3, 4, 5, 6, 7, 8], 18, true);
			graphic = coinSprite;
		}
		
		override public function added():void 
		{
			super.added();
			
			coinSprite.play("Spin");
		}
		
		override public function update():void 
		{
			super.update();
		}
		
	}

}

The coin is actually supposed to be added to the player and it’s destroy function:

package entities 
{
	import net.flashpunk.Entity;
	import net.flashpunk.graphics.Image;
	import net.flashpunk.Graphic;
	import net.flashpunk.Mask;
	import net.flashpunk.FP;
	import entities.hudRed;
	import Variables;
	import entities.redEnemy;
	import entities.redEnemyStackable;
	import entities.Coin;
	
	/**
	 * ...Player's red square
	 * @author Gil
	 */
	public class RedSquare extends DraggableEntity 
	{
		[Embed(source = "../../assets/gfx/Red.png")]
		public var ART_RED:Class;
		public var redImage:Image;
		
		public var deathTime:Number = 0;
		
		public function RedSquare(x:Number=0, y:Number=0, mask:Mask = null) 
		{
			redImage = new Image(ART_RED)
			super(x, y, redImage);
			if (!mask) setHitboxTo(redImage);
			redImage.centerOrigin();
			centerOrigin();
			type = "Obstacle"
		}
		
		override public function added():void 
		{
			super.added();
			
			x = hudRed.hudRedDrag.x;
			y = hudRed.hudRedDrag.y;
			
			draggable = true;
			isDragging = true;
			onStartDrag();
			
			FP.world.add(hor);
			FP.world.add(ver);
		}
		
		override public function update():void 
		{
			super.update();
			
			// force it to remain inside left half of the screen (taking origin into account)
			if (isDragging) 
			{
				FP.clampInRect(this, -384 - originX , 0 + originY, FP.width + width, FP.height + height);
				FP.clampInRect(this, 224 + originX , 0 + originY, FP.width - width, FP.height - height);
			}
		}
		
		override public function onStartDrag():void 
		{
			super.onStartDrag();
			
			redEnemyStackable.redSqStackSet = false;
			redEnemy.redSqSet = false;
			
					
		}
		
		override public function onEndDrag():void 
		{
			super.onEndDrag();
			
			redEnemyStackable.redSqStackSet = true;
			redEnemy.redSqSet = true;
			
		}
		
		public function destroy():void //Is activated via collision with enemy.
		{
			var coin1:Entity = new Coin;
			
			if (!isDragging && !draggable) //Entity must be set and placed before deletion occurs.
			{
				FP.world.add(coin1); //Coin is added before Entity is destroyed but at (0, 0) by default.
				FP.world.remove(this); //If Entity is set and placed, deletion successful. 
			}
		}
	}	
}

(David Williams) #4

Try:

coin1.x = this.x;
coin1.y = this.y;

Right above “FP.world.add(coin1);”


(Gil) #5

OMG so simple, I didn’t use “this”. Thanks lol I feel dumb now!


(Jacob Albano) #6

Using this isn’t actually necessary; the problem is that you never set the position of the coin at all. :wink:


(Alex Larioza) #7

For future reference, the keyword “this” is typically used to differentiate between a class’s variables and variables in local scope. For example:

...
private var health:int;
...
public function setHealth(health:int):void
{
    health = health; // BAD: this just sets the parameter health to itself
    this.health = health; // GOOD: this sets the class's variable to the parameter
}

(Zachary Lewis) #8

For functions like this, I like to skip the confusion altogether and use something like this:

private var _health:int;

public function setHealth(value:int):void
{
  if (value <= 0)
  {
    destroy();
  }
  else
  {
    _health = value;
  }
}

We’re in the function setHealth(), so it’s obvious what we should be setting and what the value means.