How to make a arrow fly to your mouse and other things?


(Logan"Nogan" Carter) #1

How do you spawn and make the arrow fly to your mouse whatever i try the player turns into the arrow and just sits their.


(Jean) #2

Post your code, the forum is for helping and learning, not just for giving codes.


(Logan"Nogan" Carter) #3

Ok here is the arrow class

package { import net.flashpunk.Entity; import net.flashpunk.FP import net.flashpunk.utils.Input; import net.flashpunk.utils.Key

public class ARROW extends Entity
{
	[Embed(source = "../New Folder/archer_elfA.png")] public var AR:Class;
	[Embed(source = "../New Folder/archer_elfA2.png")] public var AR2:Class;
	
	protected const ARROW_SPEED:Number = 50;
	protected var _velocityX:Number;
	protected var _velocityY:Number;
	public function ARROW() 
	{
		Image(AR).angle = angle;
	}
	override public function init(x:Number, y:Number, angle:Number):ARROW
	{
		this.x = x;
		this.y = y;
		_velocityX = ARROW_SPEED * Math.cos(angle * FP.RAD);
		_velocityY = ARROW_SPEED * Math.sin(angle * FP.RAD);
		
		return this;
	}
	override public function update():void
	{
		if (Input.pressed(Mouse))
		x += _velocityX * FP.elapsed;
		y += _velocityY * FP.elapsed;
	}
}

} And here is the Entity class

package { import net.flashpunk.Entity; import net.flashpunk.Graphic; import net.flashpunk.graphics.Image; import net.flashpunk.graphics.Stamp; import net.flashpunk.utils.Input; import net.flashpunk.utils.Key; import net.flashpunk.FP;

public class MyEntity extends Entity
{
	[Embed(source = "../New Folder/archer_elf.png")] private var PLAYER:Class;
	[Embed(source = "../New Folder/archer_elf2.png")] private var PLAYERL:Class;
	[Embed(source = "../New Folder/archer_elfENEN.png")] private var PLAYERE:Class;
	public function MyEntity() 
	{
		graphic = new Image(PLAYER);
	}
	override public function update():void
	{
		if (Input.mousePressed())
		{
			
		}
		if (Input.pressed(Key.A))
		{
			graphic = new Image(PLAYERL) //Player facing left
		}
		if (Input.pressed(Key.D))
		{
			graphic = new Image(PLAYER)// Player facing right
		}
		if (Input.check(Key.D)) { x += 3 };
		if (Input.check(Key.A)) { x -= 3 };
		if (Input.check(Key.W)) { y -= 3 };
		if (Input.check(Key.S)) { y += 3 };
	}
}

}


(Jean) #4

OK, a few things:

  • Your ARROW class never angle against the mouse, for that use FP.angle and the world properties mouseX and mouseY

  • Your ARROW class never has the graphic set.

  • Your ARROW class has the same graphics as the Player

  • You have set the Input check for the mouse inside the ARROW itself, but what you probably want to do is this:

      override public function update():void
      {
        if (Input.pressed(Mouse))
        x += _velocityX * FP.elapsed;
        y += _velocityY * FP.elapsed;
      }
    

Should be this:

    override public function update():void
    {
      x += _velocityX * FP.elapsed;
      y += _velocityY * FP.elapsed;
    }

And what you probably want to do is this on your MyEntity class:

if (Input.mousePressed())
{

}

Should be that:

if (Input.mousePressed())
{
  this.world.add(new ARROW());
}

(Logan"Nogan" Carter) #5

So should i also put the velocity and speed in the world class to? The graphics are different just the names are just slightly different.


(Jean) #6

No need for that, you can always get the current world with this.world so you can make all the calculation inside your ARROW class.

And sorry, now I saw that it have an “A” as suffix in the image.


(Logan"Nogan" Carter) #7

Ok thanks for your help.