dcsimg
A Flash Developer Resource Site

Results 1 to 9 of 9

Thread: two objects moving around with mouse

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    11

    two objects moving around with mouse

    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?

    Here are the attached files.

    Also, here is the actionscript code:
    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_COMPL ETE,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[r]) as Class;

    var newObject:MovieClip = new classRef();

    newObject.typestr = "good";

    } else {

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

    classRef = getDefinitionByName(goodObjects[r]) 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[i].y += speed;

    if (objects[i].y > 425) {

    removeChild(objects[i]);

    objects.splice(i,1);

    }

    if (objects[i].hitTestObject(girlmouthfront)) {

    if (objects[i].typestr == "good") {

    score += 5;

    } else {

    score += 5;

    }

    if (score < 0) score = 0;

    scoreDisplay.text = "Score: "+score;

    removeChild(objects[i]);

    objects.splice(i,1);

    }

    }



    girlmouthfront.x = mouseX;

    }





    }

    }

    I got this code from http://flashgameu.com/Catching_Game_...12-131621.html.
    Attached Images Attached Images

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Please format code with [code] tags.

    Make the mouth a child of the girl, then move the girl. The mouth will move along with her.

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    11
    like this?

    [public function CatchingSkittles() {
    girlmouthfront = new GirlMouthFront();
    girlmouthfront.y = 258.00;
    girlmouth = new GirlMouth();
    girlmouth.addChild(girlmouthfront);
    setNextObject();
    addEventListener(Event.ENTER_FRAME, moveObjects);
    }
    ]

    I did that and it seems to work. However, the images aren't showing up.

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    No, actually put [code] before your code and [/code] after. Like this:
    Code:
    public function CatchingSkittles():void{
      //stuff
    }
    You still need to add girlmouth to the display list in order for it and its children to show up.

  5. #5
    Junior Member
    Join Date
    Nov 2011
    Posts
    11
    Thanks!

    Display list? is that the display code under public class CatchingSkittles extends Movieclip?

    Code:
    package {
    	import flash.display.*;
    	import flash.events.*;
    	import flash.text.*;
    	import flash.utils.Timer;
    	import flash.utils.getDefinitionByName;
    	
    	public class CatchingSkittles extends MovieClip {
    		var girlmouth:GirlMouth;
    		var girlmouthfront:GirlMouthFront;
    		var nextObject:Timer;
    		var objects:Array = new Array();
    		var score:int = 0;
    		const speed:Number = 7.0;
    and why should I put :void in this code?

    Code:
    public function CatchingSkittles();

  6. #6
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    The display list is everything that shows up on stage. Everything that, if you go up parent by parent, you get to the stage. To put something on the display list, add it as a child of something that already is on the display list. In this case, you need to addChild(girlmouth).

    And you shouldn't actually put void there. I didn't realize it was the constructor of a class. You can, but I find that case a little odd. In general though, you explicitly put the return type of a function in the declaration. If the function doesn't return anything, use void.

  7. #7
    Junior Member
    Join Date
    Nov 2011
    Posts
    11
    Thanks! the objects showed up. However, one object (girlmouth) is above the other object (girlmouthfront). I tried to adjust the height of girlmouth, but it moved both objects down instead of just the first object. Here's the code:

    Code:
    public function CatchingSkittles() {
    			girlmouthfront = new GirlMouthFront();
    			girlmouthfront.y = 258.00;
    			girlmouth = new GirlMouth();
    			girlmouth.y = 166.60;
    			addChild(girlmouth);
    			girlmouth.addChild(girlmouthfront);
    			setNextObject();
    			addEventListener(Event.ENTER_FRAME, moveObjects);
    		}

  8. #8
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Yes, of course it moved both objects. That was the point of your question, how to get them to move together.

    A display object's coordinates are relative to its parent. So by setting girlmouthfront.y = 258, you've told it to be 258 pixels below the origin of its parent, girlmouth.

  9. #9
    Junior Member
    Join Date
    Nov 2011
    Posts
    11
    Thank you very much for your help!!!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center