A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: remove MC after hittest

Hybrid View

  1. #1
    lemon juice hurts your eyes florianvanthuyn's Avatar
    Join Date
    Jul 2005
    Location
    Merelbeke, Belgium
    Posts
    546

    remove MC after hittest

    Hi all!

    I'm making a little flash game for a school assignment, and I'm pretty much done with the coding, except for one thing:

    the hittest and the removing of the MC that got hit by something.

    I'll first explain the situation:

    The game is for a 7 year old (that was obligated) and it's made in one frame (so far) with in it an animal with a basket (which can be moved to the left and the right with the arrow keys), a background and a fruit MC which contains several frames, each representing a different object.

    The fruit is placed through a class Fruit, so I have to call a new Fruit(x,y); to het one on stage.

    This is the code which attaches instances to the stage, and tweens them to fall down.

    Code:
    var numFruit:uint = 5;
    var speed:uint = 10;
    
    var t:Timer = new Timer(3000, numFruit);
    t.addEventListener(TimerEvent.TIMER, tikker);
    
    t.start();
    
    function tikker(event:TimerEvent)
    {
    // 46 is the width of the fruit MC and 30, its height
    var fruit:Fruit = new Fruit((Math.random()*stage.stageWidth - 46),-30);
    this.addChild(fruit);
    
    var fTween:Tween = new Tween(fruit,"y",null,fruit.y,stage.stageHeight,speed,true);
    }
    The object of the game of to catch whatever falls from the sky in the basket.

    What I would like to know is:
    - how do I start a hittest check for every object (no matter what shape it is) falling from the sky?
    - how do I remove that object when a true is returned?
    Florian Vanthuyne

    WAR AGAINST SOLVED THREADS
    mark yours as Resolved under Thread Tools!

  2. #2
    lemon juice hurts your eyes florianvanthuyn's Avatar
    Join Date
    Jul 2005
    Location
    Merelbeke, Belgium
    Posts
    546
    Oh, almost forgot ..
    The animal is called m and the hitzone is inside of it and is called area.

    If you think you know how to fix this or what's definately wrong with my code or if you want to see the class code, please post a reply and I'll get back to you as soon as I can!
    Florian Vanthuyne

    WAR AGAINST SOLVED THREADS
    mark yours as Resolved under Thread Tools!

  3. #3
    lemon juice hurts your eyes florianvanthuyn's Avatar
    Join Date
    Jul 2005
    Location
    Merelbeke, Belgium
    Posts
    546
    Since I don't seem to get any response, I'll post some more of the AS I'm using.
    I've succeeded in removing them, but when the first one hits the character, I get a stream of errors

    This is the error btw:

    TypeError: Error #1009: Cannot access a property or method of a null object reference at Fruit/checkHit()

    The code which I'm using to make new Fruit objects can be found in my first post, and here's the Fruit class:

    Code:
    package 
    {
    	import flash.display.MovieClip;
    	import flash.events.Event;
    
    	public class Fruit extends MovieClip
    	{
    		var score:uint = 0;
    		
    		public function Fruit(xPos:Number, yPos:Number)
    		{
    			this.x = xPos;
    			this.y = yPos;
    
    			gotoAndStop(Math.ceil(Math.random()*3));
    			addEventListener(Event.ENTER_FRAME, checkHit);
    		}
    		
    		public function checkHit(e:Event)
    		{
    			if(this.y > MovieClip(parent).m.y)
    			{
    				MovieClip(parent).removeChild(this);
    			}
    		}
    	}
    }
    Florian Vanthuyne

    WAR AGAINST SOLVED THREADS
    mark yours as Resolved under Thread Tools!

  4. #4
    Amazed and Amused Mazoonist's Avatar
    Join Date
    Mar 2006
    Location
    Northern California
    Posts
    201
    The reason for all the errors you're getting is that before you remove the object, you must first remove the event listener for the enter frame. So add in this line:
    removeEventListener(Event.ENTER_FRAME, checkHit);

    Also, in your other code, you must declare the tween variables outside of the function that uses them, or they will get garbage collected, sometimes before they have a chance to complete:
    Code:
    //declare the variable outside of the function:
    var fTween:Tween;
    //then, in the function:
    function tikker(event:TimerEvent) 
    {
         //other code
         fTween = new Tween(.... etc);
    }

  5. #5
    lemon juice hurts your eyes florianvanthuyn's Avatar
    Join Date
    Jul 2005
    Location
    Merelbeke, Belgium
    Posts
    546
    Thanks Mazoonist! That cleared out something I actually should have found on my own, since it's something we do almost every day in class, but still .. I always seem to attract those stupid little script errors ..

    But thanks again, I'll post my future problems (yes, I'm always very optimistic) here as well, when they arive (and they will! )
    Florian Vanthuyne

    WAR AGAINST SOLVED THREADS
    mark yours as Resolved under Thread Tools!

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