Skip to main content
Inspiring
November 26, 2011
Question

two objects moving around with mouse

  • November 26, 2011
  • 1 reply
  • 1273 views

I'm making a catching game and I only wanted the falling objects hit certain area on my catcher. My catcher is a girl with mouth open and I wanted the falling objects to hit only the mouth, not any other parts of her body like arms. So I made two objects, the full girl and just the mouth. How do I make the two objects (movieclips) move around with the mouse?

I tried to embed one object into another but it didn't work....

This is actionscript I wrote so far:

package {

    import flash.display.*;

    import flash.events.*;

    import flash.text.*;

    import flash.utils.Timer;

    import flash.utils.getDefinitionByName;

   

    public class CatchingSkittles extends MovieClip {

        var girlmouthfront:GirlMouthFront;

        var girlmouth:GirlMouth;

        var nextObject:Timer;

        var objects:Array = new Array();

        var score:int = 0;

        const speed:Number = 7.0;

       

        public function CatchingSkittles() {

            girlmouthfront = new GirlMouthFront();

            girlmouthfront.y = 258.00;

            addChild(girlmouthfront);

            setNextObject();

            addEventListener(Event.ENTER_FRAME, moveObjects);

        }

       

        public function setNextObject() {

            nextObject = new Timer(1000+Math.random()*1000,1);

            nextObject.addEventListener(TimerEvent.TIMER_COMPLETE,newObject);

            nextObject.start();

        }

       

        public function newObject(e:Event) {

            var goodObjects:Array = ["Red","Purple","Yellow","Orange","Green"];

           

            if (Math.random() < .5) {

                var r:int = Math.floor(Math.random()*goodObjects.length);

                var classRef:Class = getDefinitionByName(goodObjects) as Class;

                var newObject:MovieClip = new classRef();

                newObject.typestr = "good";

            } else {

                r = Math.floor(Math.random()*goodObjects.length);

                classRef = getDefinitionByName(goodObjects) as Class;

                newObject = new classRef();

                newObject.typestr = "good";

            }

            newObject.x = Math.random()*500;

            addChild(newObject);

            objects.push(newObject);

            setNextObject();

        }

       

        public function moveObjects(e:Event) {

            for(var i:int=objects.length-1;i>=0;i--) {

                objects.y += speed;

                if (objects.y > 425) {

                    removeChild(objects);

                    objects.splice(i,1);

                }

                if (objects.hitTestObject(girlmouthfront)) {

                    if (objects.typestr == "good") {

                        score += 5;

                    } else {

                        score += 5;

                    }

                    if (score < 0) score = 0;

                    scoreDisplay.text = "Score: "+score;

                    removeChild(objects);

                    objects.splice(i,1);

                }

            }

           

            girlmouthfront.x = mouseX;

        }

       

       

    }

}

This topic has been closed for replies.

1 reply

kglad
Community Expert
Community Expert
November 26, 2011

what's the class of your girl?  does she have a child movieclip with an instance name?

kikibresAuthor
Inspiring
November 26, 2011

the picture with just the mouth is "girlmouthfront" and the picture with the girl and the mouth is "girlmouth". The "girlmouthfront" catch the falling objects. They are both movieclips. Should I add an instance name to each movieclip?

kglad
Community Expert
Community Expert
November 26, 2011

does girlmouth have a class?  if yes, what is it?  if no, it needs one.

is girlmouthfront movieclip a child or girlmouth?  if yes, what's its instance name?  if no, it needs to be one.