Skip to main content
May 6, 2013
Question

Going to another frame after all items are in place

  • May 6, 2013
  • 1 reply
  • 1343 views

Hello All!

I'm making some simple drag'n'drop games and am using this code to make it work:

package

{

    import flash.display.Sprite;

    import flash.events.MouseEvent;

    public class Main extends Sprite

    {

        var xPos:int;

        var yPos:int;

        public function Main():void

        {

            addListeners(s1, s2, s3, i1, i2, i3, n1, n2, n3, p1, p2, p3, b1, b2, b3, f1, f2, f3);

        }

        private function getPosition(target:Object):void

        {

            xPos = target.x;

            yPos = target.y;

        }

        private function dragObject(e:MouseEvent):void

        {

            getPosition(e.target);

            e.target.startDrag(true);

        }

        private function stopDragObject(e:MouseEvent):void

        {

            if (e.target.hitTestObject(getChildByName(e.target.name + "Target")))

            {

                e.target.x = getChildByName(e.target.name + "Target").x;

                e.target.y = getChildByName(e.target.name + "Target").y;

            }

            else

            {

                e.target.x = xPos;

                e.target.y = yPos;

            }

            e.target.stopDrag();

        }

        private function addListeners(... objects):void

        {

            for (var i:int = 0; i < objects.length; i++)

            {

                objects.addEventListener(MouseEvent.MOUSE_DOWN, dragObject);

                objects.addEventListener(MouseEvent.MOUSE_UP, stopDragObject);

            }

        }

    }

}

s1, s2, s3, i1, i2, i3, n1, n2, n3, p1, p2, p3, b1, b2, b3, f1, f2, f3 are objects that need to be placed correctly to win. So when they are placed correctly, what do I do to make the game recognize that the player did everything right? I'd like the game to be able to take it to another frame where a "win" graphic can be seen.

Any suggestions?

This topic has been closed for replies.

1 reply

Ned Murphy
Legend
May 6, 2013

You basically need to think of some way of checking how many objects are properly planted. 

You could do this by simply keeping a count if you make this game such that once an object is planted it cannot be replanted.  So in the conditional that places the object to match the target location you just increment that count.  When the count reaches the total, that's when it's time to move on.

Another approach could be to assign a property to each object to indicate whether it is planted or not.  When the object gets planted properly, you set that status to be true.  Then you loop thru all of the objects and see if they are all true.

May 6, 2013

Thanx Ned!

I believe that the first approach would be the one for me. Is there a code snippet for that? I'm kind of a noob

Ned Murphy
Legend
May 6, 2013

Hey Amy, thanx for your reply!

I just had to change dropDragObject to stopDragObject so that that piece of code works, but it says

Line 391120: Access of undefined property correctCount.

You need to declare that variable and you should start it off by assigning it a value of 0.

var correctCount:int = 0;