1 Replies - 3694 Views - Last Post: 01 December 2012 - 11:16 AM

#1 KBoogle  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 96
  • Joined: 15-April 10

AS3 - How to prevent clicking on empty stage or at least identify it?

Posted 23 November 2012 - 11:48 PM

I've added a number of movieclip instances (a simple image of an apple)on to the stage at random x and y. Now I am trying to click on each one to remove them.

Here is my code:

public function Apples() {

        for(var count:int=1; count<=10; count++){
            var apple = new Apple();
            apple.x = Math.random() * stage.stageWidth;
            apple.y = Math.random() * stage.stageHeight;    
            apple.name = count;
            stage.addChild(apple);
            }

stage.addEventListener(MouseEvent.CLICK, onclick);



        function onclick(e:MouseEvent):void{
            var displayObject:DisplayObject = (DisplayObject) (e.target);
            displayObject.parent.removeChild(displayObject);
            }
}


My problem is that I want to be able to figure out when all of the apples have been removed.

I've tried by adding a simple counter on every onclick call, so that once it reaches the same number of apples this is where I can display a game over message, but the problem here is that if you click on the empty stage rather than the apple, then the count still increases.

Is there a way to prevent the user from clicking on the stage, or a way to identify if the stage has been clicked on for an if condition?

Or maybe there's an entirely other way to achieve what I need?

Thanks for your advice.

Is This A Good Question/Topic? 0
  • +

Replies To: AS3 - How to prevent clicking on empty stage or at least identify it?

#2 chinchang  Icon User is offline

  • Indie Game Developer
  • member icon

Reputation: 192
  • View blog
  • Posts: 725
  • Joined: 22-December 08

Re: AS3 - How to prevent clicking on empty stage or at least identify it?

Posted 01 December 2012 - 11:16 AM

Attach the click event on the apples rather than the stage.

Even if you want to keep the way it is, inside the onclick function, check if e.target is an apple or not. If its an apple, only then remove it and decrease a counter. For checking if its an apple or not, you could give them sensible names:
apple.name = 'apple' + count;


Then inside onclick check the name of the target if it contains the string 'apple'.

Hope it helps.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1