Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 04-28-2012, 12:26 PM   #1
passionsdemise
Registered User
 
Join Date: Apr 2012
Posts: 13
Default Continuing a Function after MouseEvent

Hello All... Pretty new to actionscript 3 and only used actionscript previously for simple menu and button programming.

Currently I am working on game programming for College and I am having a little difficulty.

I am using a Block Breaker game tutorial to get the understanding of AS3 and have followed it to the letter to a point, but now I want to do something not offered in the tutorial. I do not want my ball to start moving until the Player clicks his mouse button.

originally the listener was set to Event.ENTER_FRAME and that worked to start the ball immediately.

I changed it to:
Code:
mcBall.addEventListener(MouseEvent.CLICK, moveball);
and couldnt get it to work.... So I adjusted it to
Code:
stage.addEventListener(MouseEvent.CLICK, moveball);
and now It will move the ball slightly each time I click the mouse. I want the movements to continue AFTER the click, and not just activate each time I click the button.

Here is the function code for "moveball"

Code:
//Ball Movement Function
function moveball (event:MouseEvent):void{
	mcBall.x += ballXSpeed
	mcBall.y += ballYSpeed
	//Rightside Bounce
	if(mcBall.x >= stage.stageWidth-mcBall.width){
		ballXSpeed *= -1
	}
	//leftside Bounce
	if(mcBall.x <= 0){
		ballXSpeed *= -1
	}
	//bottom Bounce
	if(mcBall.y >= stage.stageHeight-mcBall.height){
		ballYSpeed *= -1
	}
	//top Bounce
	if(mcBall.y <= 0){
		ballYSpeed *= -1
	}
	if(mcBall.hitTestObject(mcPaddle)){
		calcBallAngle();
	}
}
Any help would be appreciated Thankyou
passionsdemise is offline   Reply With Quote
Old 04-28-2012, 12:37 PM   #2
jsprclimber123
Breaking things...
 
Join Date: Jan 2010
Posts: 635
Default

If I understand correctly, you just want to delay the ball's movement starting from as soon as the movie loads to when the user first clicks the stage, correct?

If so, just add your ENTER_FRAME event to the MouseEvent function, making sure to use a Boolean variable so that you only attach the ENTER_FRAME event once.

Something like this should work:

ActionScript Code:
var startedMove:Boolean = false; stage.addEventListener(MouseEvent.CLICK, stageClicked); function stageClicked(event:MouseEvent):void{     if(!startedMove){         stage.addEventListener(Event.ENTER_FRAME, moveball);         startedMove = true;     } } function moveball (event:Event):void{     mcBall.x += ballXSpeed     mcBall.y += ballYSpeed     //Rightside Bounce     if(mcBall.x >= stage.stageWidth-mcBall.width){         ballXSpeed *= -1     }     //leftside Bounce     if(mcBall.x <= 0){         ballXSpeed *= -1     }     //bottom Bounce     if(mcBall.y >= stage.stageHeight-mcBall.height){         ballYSpeed *= -1     }     //top Bounce     if(mcBall.y <= 0){         ballYSpeed *= -1     }     if(mcBall.hitTestObject(mcPaddle)){         calcBallAngle();     } }
jsprclimber123 is offline   Reply With Quote
Old 04-28-2012, 12:46 PM   #3
passionsdemise
Registered User
 
Join Date: Apr 2012
Posts: 13
Default

My God....

That worked a treat!

Thankyou very much. I Didn't consider making a seperate function for the mouse click to initiate moveball....

This will help greatly.
passionsdemise is offline   Reply With Quote
Old 04-28-2012, 12:52 PM   #4
jsprclimber123
Breaking things...
 
Join Date: Jan 2010
Posts: 635
Default

No problem, good luck with the rest of your project!
jsprclimber123 is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 05:39 AM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.