A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: hitTestPoint Problem

  1. #1
    Member
    Join Date
    Apr 2008
    Posts
    71

    hitTestPoint Problem

    I am trying to create a space game where you have to fly around black holes. I have made a difficulty setting where easy has less black holes and hard has more. In creating the difficulty setting my hitTestPoint code has stopped working and i'm not sure why. Here is the code i am using -

    Code:
    difficulty.addEventListener(MouseEvent.CLICK, chooseDifficulty);
    function chooseDifficulty(evt:MouseEvent):void{
    	var easy:Easy = new Easy();
    	var normal:Normal = new Normal();
    	var hard:Hard = new Hard();
    	easy.x = 210;
    	easy.y = 260;
    	normal.x = 210;
    	normal.y = 300;
    	hard.x = 210;
    	hard.y = 340;
    	addChild(easy);
    	addChild(normal);
    	addChild(hard);
    	removeChild(startButton);
    	removeChild(difficulty);
    	removeChild(exit);
    	
    	easy.addEventListener(MouseEvent.CLICK, chooseEasy);
    	function chooseEasy(evt:MouseEvent):void{
    		for(var i:int = 0; i < 5; i++)
    	{
    		var blackHole:BlackHole = new BlackHole();
    		blackHole.name = "BlackHole" +i;
    		blackHole.x = Math.random() * stage.stageWidth;
    		blackHole.y = Math.random() * stage.stageHeight;
    		addChild(blackHole);
    	}
    	splashScreen.visible = false;
    	timer.start();
    	addChild(spaceship);
    	spaceship.x = 80;
    	spaceship.y = 80;
    	removeChild(easy);
    	removeChild(normal);
    	removeChild(hard);
    	}
    }
    
    stage.addEventListener(Event.ENTER_FRAME, hitTest)
    function hitTest(evt:Event):void{
    	if (spaceship.hitTestPoint(blackHole.x, blackHole.y, true))
    	{
    	removeChild(spaceship);
    	}
    }
    Any ideas?

  2. #2
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    The hittest script is executed before blackHole is created.
    - The right of the People to create Flash movies shall not be infringed. -

  3. #3
    Member
    Join Date
    Apr 2008
    Posts
    71
    im not sure i understand?

  4. #4
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    This part
    stage.addEventListener(Event.ENTER_FRAME, hitTest)
    with its function is compiled when the blackHole instances are not yet created. Actually you should get an error message that blackHole is undefined.
    - The right of the People to create Flash movies shall not be infringed. -

  5. #5
    Member
    Join Date
    Apr 2008
    Posts
    71
    ok. thanks for your help. i'm still not sure how to go about fixing this, any suggestions?

  6. #6
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    One possibility is to pack the whole stage function into a function and execute it after the blackHole instances are created.

    addChild(blackHole);
    }
    bhExecute();


    ......


    function bhExecute():void
    {
    stage......
    ...
    }
    - The right of the People to create Flash movies shall not be infringed. -

  7. #7
    Member
    Join Date
    Apr 2008
    Posts
    71
    ok, i managed to get it working but now it only works on one instance of the blackHole movieclip. so the spaceship will fly over all of the blackholes fine apart from one, which will remove it.

  8. #8
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    ...because blackHole will refer to the last instance made. If you want it referred to all instances one possibility is to put the function within the for loop or call all blackHole instances by name using a for loop in the listener function.
    - The right of the People to create Flash movies shall not be infringed. -

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