dcsimg
A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: virtual instance of MC

  1. #1
    Junior Member
    Join Date
    May 2008
    Posts
    11

    virtual instance of MC

    Howayee, greetings from ireland where it is uncharacteristically hot today.

    My friend and I are creating a simple game for college where coins fall from the sky randomly and a pig can collect points when they collide. I have the coins falling ok but am having trouble applying a hit test to when the coins collide with the pig.

    The coin only exists in the library and is actionscripted onto the stage

    my problem is that i dont know how to do a hit test when the MC is not physically on the stage (therefore you cant give the MC an instance name)


    Any help that anyone might have would be greatly appreciated.

    Here is the script for the falling coins (the coind is called snowflake!):

    stage.addEventListener(Event.ENTER_FRAME, snowflake);
    var count:int=0;
    var goal:int=int(Math.random()*50+50);

    function snowflake (event:Event):void
    {
    count++;
    if(count>=goal){
    count=0;
    var _Snowflake:mcSnowflake;

    _Snowflake = new mcSnowflake();
    addChild(_Snowflake);
    _Snowflake.x = Math.random() * 510;
    _Snowflake.y = Math.random() * -60;



    stage.addEventListener (Event.ENTER_FRAME, snowfall);
    }

    function snowfall(event:Event):void
    {
    if ( _Snowflake.y> 450 || _Snowflake.x> 550)
    {
    stage.removeEventListener(Event.ENTER_FRAME, snowfall);
    removeChild(_Snowflake);
    }
    else
    {
    _Snowflake.y += 5;
    }
    }
    }


    so all i need is to do a hit test between piggy (whos on the stage) and the actionscripted coins.

    I attack an fla file
    thanks for reading,
    b
    Attached Files Attached Files

  2. #2
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    You are confusing the name and the actual coin-object, read this and this.

  3. #3
    Junior Member
    Join Date
    May 2008
    Posts
    11

    hi there

    hi there,

    Thanks so much for helping me, those were the perfect threads.


    I've been at it since to no avail though. It wrecking my head! I used the variable name _snow flake in the hittest and now although there are no errors, the output box just goes mad with outputs that arent supposed to be there. Well first there are no outputs and then it goes mad.

    I attached an example zipped fla if you could take a look i'd be very happy
    snowflake is a movie clip of a coin falling, Piggy is a pig

    Thanks for your help




    The code i used is as follows in case you dont want to look at the fla:



    // initialize arrow variables
    var leftArrow:Boolean = false;
    var rightArrow:Boolean = false;
    var upArrow:Boolean = false;
    var downArrow:Boolean = false;

    // set event listeners
    stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressedDown);
    stage.addEventListener(KeyboardEvent.KEY_UP, keyPressedUp);
    stage.addEventListener(Event.ENTER_FRAME, moveMascot);

    // set arrow variables to true
    function keyPressedDown(event:KeyboardEvent) {
    if (event.keyCode == 37) {
    leftArrow = true;
    } else if (event.keyCode == 39) {
    rightArrow = true;
    }
    }

    // set arrow variables to false
    function keyPressedUp(event:KeyboardEvent) {
    if (event.keyCode == 37) {
    leftArrow = false;
    } else if (event.keyCode == 39) {
    rightArrow = false;
    }
    }

    // move every frame
    function moveMascot(event:Event) {
    var speed:Number = 5;

    if (leftArrow) {
    piggy_mc.x -= speed;
    }
    if (rightArrow) {
    piggy_mc.x += speed;
    }
    if (upArrow) {
    piggy_mc.y -= speed;
    }
    if (downArrow) {
    piggy_mc.y += speed;
    }
    }


    stage.addEventListener(Event.ENTER_FRAME, snowflake);
    var count:int=0;
    var goal:int=int(Math.random()*50+50);

    function snowflake (event:Event):void
    {
    count++;
    if(count>=goal){
    count=0;
    var _Snowflake:mcSnowflake;

    _Snowflake = new mcSnowflake();
    addChild(_Snowflake);
    _Snowflake.x = Math.random() * 510;
    _Snowflake.y = Math.random() * -60;



    stage.addEventListener (Event.ENTER_FRAME, snowfall);
    }

    function snowfall(event:Event):void
    {
    if ( _Snowflake.y> 450 || _Snowflake.x> 550)
    {
    stage.removeEventListener(Event.ENTER_FRAME, snowfall);



    //HERE IS THE HITTEST, I USED THE VARIABLE _Snowflake FROM ABOVE

    _Snowflake.addEventListener (Event.ENTER_FRAME, enter_frame) ;
    function enter_frame (event:Event) :void{
    if (_Snowflake.hitTestObject(piggy_mc)) {
    trace ("piggy got coin")
    }
    }



    removeChild(_Snowflake);
    }
    else
    {
    _Snowflake.y += 5;
    }
    }
    }
    Attached Files Attached Files

  4. #4
    Junior Member
    Join Date
    May 2008
    Posts
    11

    hi

    Can someone please help me!!

  5. #5
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    FYI, you can format your code with [ code ] or [ php ] tags.

    You cannot use _Snowflake in the snowfall function because _Snowflake is local to the snowflake function. You should also not define functions inside of other functions, because that puts them in a limited scope, and is at best inefficient (nb: anonymous functions are valid as an advanced technique, but that is unrelated to what you were trying to do)

    Can you post the code with proper formatting? it'll be easier to spot scope issues that way.

  6. #6
    Amazed and Amused Mazoonist's Avatar
    Join Date
    Mar 2006
    Location
    Northern California
    Posts
    201
    Hey flashfiddler,

    I'd like to help. I downloaded your file and have been re-working it:
    Code:
    // initialize arrow variables
    var leftArrow:Boolean = false;
    var rightArrow:Boolean = false;
    var upArrow:Boolean = false;
    var downArrow:Boolean = false;
    // set event listeners
    stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressedDown);
    stage.addEventListener(KeyboardEvent.KEY_UP, keyPressedUp);
    stage.addEventListener(Event.ENTER_FRAME, moveMascot);
    // set arrow variables to true
    function keyPressedDown(event:KeyboardEvent) {
    	if (event.keyCode == 37) {
    		leftArrow = true;
    	} else if (event.keyCode == 39) {
    		rightArrow = true;
    	}
    }
    // set arrow variables to false
    function keyPressedUp(event:KeyboardEvent) {
    	if (event.keyCode == 37) {
    		leftArrow = false;
    	} else if (event.keyCode == 39) {
    		rightArrow = false;
    	}
    }
    // move every frame
    function moveMascot(event:Event) {
    	var speed:Number = 5;
    
    	if (leftArrow) {
    		piggy_mc.x -= speed;
    	}
    	if (rightArrow) {
    		piggy_mc.x += speed;
    	}
    	if (upArrow) {
    		piggy_mc.y -= speed;
    	}
    	if (downArrow) {
    		piggy_mc.y += speed;
    	}
    }
    
    //var count:int=0;
    var goal:int=int(Math.random()*50+50);
    var timer:Timer = new Timer(1000, goal);
    timer.addEventListener(TimerEvent.TIMER, createSnowflakes);
    timer.start();
    
    function createSnowflakes(event:TimerEvent):void {
    	var _Snowflake:mcSnowflake = new mcSnowflake();
    	_Snowflake.x = Math.random() * 510;
    	_Snowflake.y = Math.random() * -60;
    	addChild(_Snowflake);
    	_Snowflake.addEventListener(Event.ENTER_FRAME, snowFlakeFalling);
    }
    
    function snowFlakeFalling(event:Event):void {
    	var thisFlake:mcSnowflake = mcSnowflake(event.target);
    	if(thisFlake.y > 450) {
    		thisFlake.removeEventListener(Event.ENTER_FRAME, snowFlakeFalling);
    		removeChild(thisFlake);
    	}
    	if (thisFlake.hitTestObject(piggy_mc)){
    		trace("piggy got coin");
    		thisFlake.removeEventListener(Event.ENTER_FRAME, snowFlakeFalling);
    		removeChild(thisFlake);
    	}else {
    		thisFlake.y += 5;
    	}
    }
    I didn't change the piggy or his motion, or the arrow key control, all that is fine. About half way down the code block above is where I started changing things. I introduced a timer object to handle adding new snowflakes. The 1000 in that constructor is milliseconds, which means there will be a new snowflake added every second. The second parameter there is how many repetitions, which I fed the result of the random number you were generating. So there will be anywhere from 50 to 100 snowflakes.

    Since the timer allows you to specify a repeat count, the "count" variable you had is no longer needed so I commented it out. The timer gets an event listener which tells it to call the createSnowFlakes function. Each time this function is called, it creates a new snowflake, which is given a variable name of _Snowflake, just like what you had. The thing to realize is that this variable is local to this function, so when the function completes, that name will no longer be available. So anything we want to do with the current snowflake (named _Snowflake) had better be done inside this function. The thing to do is to give the snowflake its own listener for the ENTER_FRAME event (not the stage). You'll see why as I explain further.

    So what you have is a whole bunch of snowflakes, and when they're generated, every one of them is given the temporary name "_Snowflake," and that name is used to assign each one an event listener. When this function completes, it's generated a snowflake and added an ENTER_FRAME listener to it, and as you'll see, we no longer will need to hang on to the name, there's another way to refer to it inside the handler function that it's been assigned.

    Each snowflake is given a copy of "snowFlakeFalling" as its ENTER_FRAME handler function. Within the handler function, we can refer to a particular snowflake as "event.target." This is a way of saying "whichever snowflake you are." This is a very important concept, as this anonymous way of referring to objects allows you to do all kinds of cool things. Inside "snowFlakeFalling," just to make it easier to refer to the event.target, I created a local variable and called it "thisFlake" and made it equal to event.target which I "cast" to the mcSnowflake type. Now you've got a reference to a particular snowflake, and you didn't have to hang onto the name of it from the other function.

    Since each snowflake is given an ENTER_FRAME event handler using addEventListener, you will need to remove that event listener from the object before you attempt to use removeChild on it, otherwise you get some really nasty error messages.

    Anyway, I hope all that helps!

  7. #7
    Junior Member
    Join Date
    May 2008
    Posts
    11

    hi

    thanks so much,

    That really helped, i've been working on it since and have figured out scores and lives with the hittest. I was really stuck on that question for ages and really appreciate the time you took to answer it for me and the depth of the explanation.

    Thanks a million
    b

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