0

So I made a game. There is a Man and a Sword. You can drag the sword. I have an idea:

If I drag the sword, and I move it to man, then the man will pick it up. If I don't move it to man, then the sword teleporting back to the first position.

enter image description here

How can i programm this? I Tried this, but doesn't work:

on (press) {
startDrag(this);
}
on (release) {
stopDrag();
if (_root.vcam.inventory.inventory_items.sword1._y == 90 - 270 & _root.vcam.inventory.inventory_items.sword1._x == -430 - -350)
{
_root.man.sword._visible = true;
}
else
{
    _root.vcam.inventory.inventory_items.sword1._x = 0;
    _root.vcam.inventory.inventory_items.sword1._y = 0;
}
}
Kárpáti András
  • 1,221
  • 1
  • 16
  • 35
  • your condition is evaluated a single time when the mouse is released and there's no time for it be met instantly. You should use an enterFrame event to check the condition on mouse press and remove that event on mouse release - essentially continuously evaluate your condition while the mouse is being dragged (has been pressed, but not released yet) – George Profenza Oct 23 '13 at 19:37
  • Not tested, but something along these lines: `on (press) { startDrag(this); this.checkSword = true;//flag to start checking } on (release) { stopDrag(); this.checkSword = false;//stop checking } on (enterFrame){//continuosuly evaluate if needed if(this.checkSword){ if (_root.vcam.inventory.inventory_items.sword1._y == 90 - 270 & _root.vcam.inventory.inventory_items.sword1._x == -430 - -350) { _root.man.sword._visible = true; } else { _root.vcam.inventory.inventory_items.sword1._x = 0; _root.vcam.inventory.inventory_items.sword1._y = 0; } } }` – George Profenza Oct 23 '13 at 19:40

1 Answers1

0

Here is an exemple :

On the sword movieclip actions :

onClipEvent (load) {
    // save original position
    var origine = {x:_x,y:_y}       

    function onPress() {
        this.startDrag(false);
        this.onEnterFrame = function() {
            // check if the sword hits the man
            if (this.hitTest(_root.man) ) {
                this.stopDrag();
                delete this.onEnterFrame;
                // make the "sword" clip of the man visible
                _root.man.sword._visible = true; 
                // hide this
                _visible = false; 
            }
        }
    }

    // releasing not on the man
    function onRelease() {
        this.stopDrag();
        this._x = origine.x;
        this._y = origine.y;
    }
}
RafH
  • 4,504
  • 2
  • 23
  • 23