Jump to content
GreenSock

Search In
  • More options...
Find results that contain...
Find results in...
Gabums

Greensock and game dev?

Recommended Posts

Hi All,

 

Have another question for everyone. I'm now trying to create a snowball fight type game. I want it to play out where the user tries to click on a target and a snowball will come out from the bottom center of the screen and be "thrown" in the direction of the click. After spending hours trying to figure this out, I decided to try greensock tweens and it got me very close. Here is the code I am using:

 

var Bullet:bullet = new bullet();
// define bullet start point and speed
Bullet.speed = 3;
Bullet.x = 360;
Bullet.y = 400;
addChild(Bullet);

//declares variables
var mousePositionX:Number = 0;
var mousePositionY:Number = 0;

//adds the event listener
stage.addEventListener(MouseEvent.CLICK, onClickHandler, false, 0, true);


//assigns variables and moves the object
function onClickHandler(event:MouseEvent):void
{
 mousePositionX = event.target.mouseX;
 mousePositionY = event.target.mouseY;
 TweenMax.to(Bullet, .5, {x:mousePositionX, y:mousePositionY});
}

 

So right now that code tells my snowball to throw, but it just stops where the mouse click was. I need the snowball to continue its way out of the stage if it misses the target and stop if it hits it. Also, if I click multiple times on the stage, the snowball just follows my mouse (and not very accurately at that), if I click the stage again I want a new snowball to come out. Sooo... my questions are, in order of importance...

1.) Is this something the greensock code can do? Or do I need to look elsewhere still?

2.) How do I get it to extend past where the mouse click was?

3.) Is there a way to tell the snowball to just pay attention to the first click. And then each click after throws a new one? I feel like there is... like I need to nest the functions or call an array but I'm not sure how to handle that.

 

Thank you again so much in advance!!!

Link to comment
Share on other sites

On second thought...I can probably make use of the fact that the snowball stops where it does. So scratch that concern...

 

If anyone has any thoughts on how to get the snowball follow the mouse click once and then on the next click make a new one...i'd be very appreciative of your help. Thanks again in advance!

Link to comment
Share on other sites

to answer the question about new bullets. you can just create new bullets each time the user clicks.

 

in the library, select your bullet symbol and assign it a class of Bullet. (http://weblog.cahlan.com/2006/08/attach ... pt-30.html)

 

in your code do this:

 

 

//assigns variables and moves the object
function onClickHandler(event:MouseEvent):void
{

//create bullet, position it and add to stage
var newBullet:Bullet = new Bullet();
newBullet.x = stage.stageWidth *.5;
addChild(newBullet);


   mousePositionX = event.target.mouseX;
   mousePositionY = event.target.mouseY;
   TweenMax.to(newBullet, .5, {x:mousePositionX, y:mousePositionY});
}

Link to comment
Share on other sites

import flash.display.MovieClip;
import com.greensock.*;
import com.greensock.easing.*;

//declares variables
var mousePositionX:Number = 0;
var mousePositionY:Number = 0;

//adds the event listener
stage.addEventListener(MouseEvent.CLICK, onClickHandler, false, 0, true);


//assigns variables and moves the object
function onClickHandler(event:MouseEvent):void
{
   mousePositionX = event.target.mouseX;
   mousePositionY = event.target.mouseY;
var mc:MovieClip = createBullet();
   TweenMax.to(mc, .5, {x:mousePositionX, y:mousePositionY});
}

function createBullet():MovieClip {
var bullet:Bullet = new Bullet();
// define bullet start point and speed
//bullet.speed = 3;
bullet.x = 275; //based on a stage width of 550, having it start in middle
bullet.y = 400;
addChild(bullet);
return bullet;

}

 

I swapped the capitalization of Bullet compared to your code but this should work. As far as having it carry through the point where you click, that may need to involve some trig and I am too far out of school to remember how to do that... ?

Link to comment
Share on other sites

Sure, there are MANY ways you could accomplish this with the tweening platform. I've attached a simplified FLA that demonstrates two techniques - one using the Physics2DPlugin (which allows you to set velocity, acceleration, friction, etc.) or a plain x/y tween. Here's the code:

 

import com.greensock.*;
import com.greensock.easing.*;
import com.greensock.plugins.*;
//TweenPlugin.activate([Physics2DPlugin]);
stage.align = "TL"; //keeps the emitting point in the same spot at the bottom of the screen

var shotDistance:Number = 520; //this is only used for the regular x/y tween
var speed:Number = 200;

//add the event listener
stage.addEventListener(MouseEvent.CLICK, onClickHandler, false, 0, true);

//assigns variables and moves the object
function onClickHandler(event:MouseEvent):void {
var bullet:Bullet = new Bullet();
bullet.x = stage.stageWidth / 2;
bullet.y = stage.stageHeight;
addChild(bullet);

//find the angle in radians to the mouse position
var angle:Number = Math.atan2(event.target.mouseY - bullet.y, event.target.mouseX - bullet.x);

//do a physics2D tween, but translate the angle from radians to degrees. Of course you can apply acceleration, friction, or whatever you want very easily. See the Physics2DPlugin ASDocs for details.
   //TweenMax.to(bullet, 3, {physics2D:{angle:angle * 180 / Math.PI, velocity:speed}});

//-OR- you could do a normal x/y tween like this:
TweenMax.to(bullet, shotDistance / speed, {x:Math.cos(angle) * shotDistance + bullet.x, y:Math.sin(angle) * shotDistance + bullet.y});

}

 

Is that what you were looking for?

Link to comment
Share on other sites

Hello again,

 

Thank you so much for providing all your suggestions and code. I have my snowballs being thrown perfectly but now there is a weird glitch. I am trying to make it that when you click on a target, the snowball will hit it and then the target disappears. Trying to test out code to make that work is being hampered by the fact that every time I click my target movie clip, the snowball goes flying off to the side, it won't fly toward the target mc. The only bit of code I edited from what was suggested above is this:

 

//assigns variables and moves the object

function onClickHandler(event:MouseEvent):void

{

mousePositionX = event.target.mouseX;

mousePositionY = event.target.mouseY;

var bullet:MovieClip = createBullet();

TweenMax.to(bullet, .5, {x:mousePositionX, y:mousePositionY, onComplete:removeSnowball});

function removeSnowball():void {

removeChild(bullet);

bullet = null;

}

}

 

But i can't see why that would be a problem. Any ideas??? Thanks again!!!

 

UPDATE: I just noticed that this is only a problem with movieclips NOT graphic symbols. Weird.

 

UPDATE UPDATE: So I have a new theory. It almost seems like when I click my target mc, the mousePositionX and mousePositionY stop registering according to the stage but registers based on the smaller target mc dimensions. Could this be the case? If so, would I then have to define mousePositionX and mousePositionY based on the stage? This doesn't work but just an example: x:stage.mousePositionX, y:stage.mousePositionY

Link to comment
Share on other sites

...It almost seems like when I click my target mc, the mousePositionX and mousePositionY stop registering according to the stage but registers based on the smaller target mc dimensions. Could this be the case? If so, would I then have to define mousePositionX and mousePositionY based on the stage? This doesn't work but just an example: x:stage.mousePositionX, y:stage.mousePositionY

Yep, that's the problem. When you did event.target.mouseX, that returns the x position of the mouse according to the event.target's LOCAL coordinates (internal). If you want the stage's mouse coordinates, you can either use stage.mouseX and stage.mouseY or MouseEvents have stageX and stageY properties.

 

So you decided not to use the code I provided that makes the bullet shoot through the target? I thought that's what you were looking for.

Link to comment
Share on other sites

Ah, ok... i guess I need to play around with using the stage X and Y positions then. Every time I have tried so far I keep getting an error that Stage isn't defined or something to that extent (I stopped working on it for the night so the error isn't in front of me). And yes, I decided not to use the code to make the snowball shoot through the target. It seems to make more sense to have it stop at the clicked point and then be removed. The other user, jgutherie, did need that code and I am glad that I was able to start the convo for them to get it. I really appreciate all the help you have been offering. Thank you!

Link to comment
Share on other sites

  • 2 weeks later...

Really cool stuff. I need to brush up on my trigonometry... soh cah toa...

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×