Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Going to another frame after all items are in place

Guest
May 06, 2013 May 06, 2013

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?

TOPICS
ActionScript
1.3K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 06, 2013 May 06, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
May 06, 2013 May 06, 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
May 06, 2013 May 06, 2013

In your branch for:

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

Put something like:

//deactivate object

e.currentTarget.removeEventListener(MouseEvent.MOUSE_DOWN, dragObject);

e.currentTarget.removeEventListener(MouseEvent.MOUSE_DOWN, dyopDragObject);

//increment correct count

correctCount++;

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
May 06, 2013 May 06, 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.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
May 06, 2013 May 06, 2013

Yes, you need to define an instance variable of correctCount on your Class.

Note you should probably mark your xPos and yPos variables as private or protected (var is by default private, but you should be seeing compiler warnings on it, and you should not get in the habit of ignoring those).

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 06, 2013 May 06, 2013

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

var correctCount:int = 0;

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
May 06, 2013 May 06, 2013

or

private var correctCount:int=0;

My preference is to mark everything protected (too much time in Flex), but since everything else in the Class is marked private, might as well be consistent.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
May 06, 2013 May 06, 2013

Ok people, my code looks like this now, there are no compiler errors, but how can this help me go to the next frame? Sorry for noobing you, been working with AS2 before, but this is my first assignment in AS3. A good way to learn something is by being pressured by deadlines

package

{

    import flash.display.Sprite;

    import flash.events.MouseEvent;

    public class Puzzle extends Sprite

    {

        private var xPos:int;

        private var yPos:int;

        private var correctCount:int = 0;

        public function Puzzle():void

        {

            addListeners(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12);

        }

        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;

                //deactivate object

                e.currentTarget.removeEventListener(MouseEvent.MOUSE_DOWN, dragObject);

                e.currentTarget.removeEventListener(MouseEvent.MOUSE_DOWN, stopDragObject);

                //increment correct count

                correctCount++;

            }

            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);

            }

        }

    }

}

p.s.: just so you know, it's a non profit thing, some games for my child's kindergarten - they are trying to introduce kids to PCs..

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
May 06, 2013 May 06, 2013

add this after

                //increment correct count

                correctCount++;

// totalCount is the amount of solutions the user must solve to proceed

if(correctCount == totalCount){

nextFrame();

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
May 06, 2013 May 06, 2013

To help you learn how to find the info you need, I'm going to send you to the reference. http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/MovieClip.html. Look for a function that will allow you to go to another frame and stop (or play, depending on your needs).  Use a conditional to see if you should go there based on the count of correct items.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
May 07, 2013 May 07, 2013
LATEST

thank you all!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines