Hi.
I wanna make it so that when I touch a weapon with my hero, he picks it up. I’ve gone back and forth on how to do this, and I even made a topic called “Colliding with a TYPE, make that TYPE run a function? [SOLVED]”, but that only covers while it is actually colliding.
I want to make it so that if it collides once, then it runs a function forever.
I have this in my hero:
private var weapon:Weapons = new Weapons();
public static var isHoldingWeapon:Boolean = false;
and then in his update code:
if (isHoldingWeapon == true)
{
weapon.updatePosition(wepHotspotX, wepHotspotY);
}
And then in the weapon on the stage:
public function Weapon_Sword(xt:Number, yt:Number)
{
layer = 1;
x = xt;
y = yt;
setHitbox(112, 112);
sprSword.add("idle", [], 0, false);
sprSword.add("attack", [0, 1, 2, 3], 20, false);
graphic = sprSword;
Input.define("Attack", Key.X);
_held = false;
}
override public function update():void
{
if (!_held)
{
var hero:Hero = Hero(collide("hero", x, y));
if (hero != null)
{
Hero.isHoldingWeapon = true;
_held = true;
}
}
super.update();
}
override public function updatePosition(wepx:Number, wepy:Number):void
{
if (Hero.isFacingLeft)
{
x = wepx - 200;
sprSword.flipped = true;
}
else
{
x = wepx;
sprSword.flipped = false;
}
this.y = wepy;
//Attack
if (Input.pressed("Attack"))
{
if (HeroStats.currentStamina == HeroStats.stamina)
{
HeroStats.currentStamina = 0;
attacking = true;
}
}
if (attacking)
{
sprSword.play("attack");
var enemy:Enemies = collide("enemy", x, y) as Enemies;
if (enemy) enemy.takeDamage(damage);
}
if (sprSword.currentAnim == "attack")
{
if (sprSword.index == 3)
{
attacking = false;
sprSword.play("idle");
}
}
}
And here is the weapons class:
public class Weapons extends Entity
{
public function Weapons(x:Number=0, y:Number=0, graphic:Graphic=null, mask:Mask=null)
{
super(x, y, graphic, mask);
type = "weapon"
}
public function updatePosition(wepx:Number, wepy:Number):void
{
}
public function dealDamage(damageTaken:Number):void
{
}
}
If I change the hero update code to this:
var weaponhold:Weapons = collide("weapon", x, y) as Weapons;
if(wepaonhold)
{
weaponhold.updatePosition(blablabla)
}
Then it works, but then sometimes the hero “drops” the weapon if I move too fast. Which means it isn’t a viable option.
Thanks