Be a Supporter!

Click, move to mouse

  • 670 Views
  • 22 Replies
New Topic Respond to this Topic
ChilliDog
ChilliDog
  • Member since: Feb. 16, 2007
  • Offline.
Forum Stats
Member
Level 16
Blank Slate
Click, move to mouse 2007-06-09 18:25:28 Reply

Ok, I've searched the forums and AS Main but I can't seem to find how to do this. It's a simple idea, I want my character to move to where the mouse clicks. I found a game with this kind of movement online, DragonFable. That's the kind of movement I want, the player clicks a spot on the screen and the character runs to that spot and stops. Any help would be greatly appreciated.

Thanks!


AS: Main

Don't read this sentence.

ZeeAk
ZeeAk
  • Member since: Mar. 7, 2006
  • Offline.
Forum Stats
Member
Level 20
Art Lover
Response to Click, move to mouse 2007-06-09 18:29:36 Reply

The DF guys, AE, have been using Flash for years. It's actually NOT that simple.

ChilliDog
ChilliDog
  • Member since: Feb. 16, 2007
  • Offline.
Forum Stats
Member
Level 16
Blank Slate
Response to Click, move to mouse 2007-06-09 18:34:24 Reply

Right so uh...thanks for your help??
I said it was a simple IDEA. I obviously have no idea how to do it so why would I make judgments about it's difficulty?


AS: Main

Don't read this sentence.

ShirkDeio
ShirkDeio
  • Member since: Mar. 29, 2007
  • Offline.
Forum Stats
Member
Level 16
Programmer
Response to Click, move to mouse 2007-06-09 18:37:26 Reply

char_mc.onLoad = function() {
the_x = 0;
the_y = 0;
};
char_mc.onMouseDown = function() {
the_x = _xmouse;
the_y = _ymouse;
};
char_mc.onEnterFrame = function() {
if(this._x < the_x) {
this._x += 5;
};
if(this._y < the_y) {
this._y += 5;
};
if(this._x > the_x) {
this._x -= 5;
};
if(this._y > the_y) {
this._y -= 5;
};
};

That's the very basic idea. char_mc is your character. When you click, the variables the_x and the_y are updated with the location of the mouse. Every frame, the code checks to see if the character's _x and _y are less/more than the_x and the_y.


I am a servant of the Most High God
The Hanging Collab || The Legend of Newgrounds (Game) || Crystal Rays (Song)

ZeeAk
ZeeAk
  • Member since: Mar. 7, 2006
  • Offline.
Forum Stats
Member
Level 20
Art Lover
Response to Click, move to mouse 2007-06-09 18:38:47 Reply

At 6/9/07 06:34 PM, ChilliDog wrote: Right so uh...thanks for your help??
I said it was a simple IDEA. I obviously have no idea how to do it so why would I make judgments about it's difficulty?

Sorry. Umm.. Do you know much AS?

ShirkDeio
ShirkDeio
  • Member since: Mar. 29, 2007
  • Offline.
Forum Stats
Member
Level 16
Programmer
Response to Click, move to mouse 2007-06-09 18:39:40 Reply

Forgot to say, if the char_mc _x or _y are less/more than the_x/the_y, it adds or subtracts a certain amount from char_mc _x/_y. That's the very basic idea.


I am a servant of the Most High God
The Hanging Collab || The Legend of Newgrounds (Game) || Crystal Rays (Song)

ShirkDeio
ShirkDeio
  • Member since: Mar. 29, 2007
  • Offline.
Forum Stats
Member
Level 16
Programmer
Response to Click, move to mouse 2007-06-09 18:41:48 Reply

Sorry, one last post for you here...

Here's my example .swf. It's how I made the code.
http://img.photobucket.com/albums/v607/LBG92/
Untitled-1.swf

Hope I helped you!


I am a servant of the Most High God
The Hanging Collab || The Legend of Newgrounds (Game) || Crystal Rays (Song)

ChilliDog
ChilliDog
  • Member since: Feb. 16, 2007
  • Offline.
Forum Stats
Member
Level 16
Blank Slate
Response to Click, move to mouse 2007-06-09 18:42:55 Reply

I'm pretty much a noob at AS, I really started working with it like a month ago, before that pretty much only knew buttons. I've been learning a lot though and before I came here I was going to try a code that made the char move to the mouse position but I figured it they moved the mouse it would mess that up. I'm going to try shirk's method, I'll post with how that goes. Thanks!


AS: Main

Don't read this sentence.

starBlinky
starBlinky
  • Member since: Sep. 24, 2004
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to Click, move to mouse 2007-06-09 18:44:39 Reply

I am writing this on my PS3 Browser, and theres probably some mistakes but try this...Make your main character a movieclip. Instance name him 'hero' Then put this code in the main timeline:
onEnterFrame = hero;
function hero(){
if(mouseDown){
y = _yMouse
x = _xMouse
}
with(hero){
_x<x ? _x++: _x--;
_y<y ? _y++: _y--;
}}

You can also do this with trig, but I am still learning how. Anyways hope that works. I think the 'mouseDown' code is wrong tho.

ChilliDog
ChilliDog
  • Member since: Feb. 16, 2007
  • Offline.
Forum Stats
Member
Level 16
Blank Slate
Response to Click, move to mouse 2007-06-09 18:54:57 Reply

Shirk, your code is working beautifully, thanks!
A couple questions though.

I have a running animation that plays and I set it so that when you click it plays the animation and I tried to set it so that when he reaches the spot (this._x = the_x, this._y = the_y) hoping that it would work but it doesn't.

So my first ? is:

How can I make it so that when my character reaches where the mouse clicked, it will go back to the standing animation?

Second ?:
Is there a way I can use _xscale = -scale and whatnot (or something else) to make it so that when the mouse clicks, depending on where the click was (left or right of the character) the character will face that way?


AS: Main

Don't read this sentence.

ChilliDog
ChilliDog
  • Member since: Feb. 16, 2007
  • Offline.
Forum Stats
Member
Level 16
Blank Slate
Response to Click, move to mouse 2007-06-09 18:56:42 Reply

Sorry, I'm tired and just realized that I left something out of that last post.

I want the running animation to STOP when the character gets to his spot.


AS: Main

Don't read this sentence.

ChilliDog
ChilliDog
  • Member since: Feb. 16, 2007
  • Offline.
Forum Stats
Member
Level 16
Blank Slate
Response to Click, move to mouse 2007-06-09 19:05:06 Reply

At 6/9/07 06:58 PM, LCurtis wrote: Can you do any AS on you're own. He just gave you how to move, now you want him to animate it with AS too. What next make him attack, then die, .... Try and do it yourself first then post the code that you tried and we will see where you went wrong.

...can you do any reading on your own?
Yes, I can do some AS, and I've tried all I can.
And no, I already animated it myself, I'm asking if there's a way to change the direction and stop the animation when the movement stops.

So like...what was the point of you posting? Not to mention posting with such bad grammar and use of punctuation. Sorry, that bothers me.
If you're not going to help then just leave, no need to be a dick just to raise your post count.
I guess I missed the memo that it's now wrong to ask for help. I've done all that I can do and I know that NG has many many skilled scripters when it comes to Flash, I'm just being resourceful. Sorry that I don't know everything there is to know about AS.

Now, if anyone CAN offer any help as to my last two questions I would greatly appreciate it.


AS: Main

Don't read this sentence.

ChilliDog
ChilliDog
  • Member since: Feb. 16, 2007
  • Offline.
Forum Stats
Member
Level 16
Blank Slate
Response to Click, move to mouse 2007-06-09 19:24:32 Reply

LOL!
Right, because I honestly don't want to learn anything. I'd much rather come here for all coding help and deal with ignorant assholes such as yourself.

Anyway, for those actually offering help, I tried again to get the left right thing to work but am not sure how to do it. This is what I currently have for the mouse click

char_mc.onMouseDown = function() {
the_x = _xmouse;
the_y = _ymouse;
if (_xmouse > _root.char_mc._x) {
_root.char_mc._xscale = scale;
}
if (_xmouse < _root.char_mc._x) {
_root.char_mc._xscale = -scale;
}
_root.char_mc.gotoAndStop(2);
};

Like I said, I'm still pretty new to AS so I tried the best that I could. I did manage to get the character to not move right when the game starts but haven't accomplished the stopping of the running animation.


AS: Main

Don't read this sentence.

ChilliDog
ChilliDog
  • Member since: Feb. 16, 2007
  • Offline.
Forum Stats
Member
Level 16
Blank Slate
Response to Click, move to mouse 2007-06-09 19:26:55 Reply

At 6/9/07 07:14 PM, LCurtis wrote: Not to mention that its very basic code to get what you want. Its like you don't want to actual learn (by reading tutorials or searching the net for solutions)

If it's so basic than let's see it.
And read the FIRST SENTENCE of my original post dumbass, I ALWAYS search before I post. There wasn't anything to learn from. Maybe you should learn to READ before you post...


AS: Main

Don't read this sentence.

InfallibleSquirrel
InfallibleSquirrel
  • Member since: Jun. 22, 2006
  • Offline.
Forum Stats
Member
Level 10
Programmer
Response to Click, move to mouse 2007-06-09 19:59:52 Reply

on the subject of rotating your hero.. Im not 100% sure how to code it and its waay too late at night for me to figure it out but ill tell you that you use the code

Math.atan2
//not sure but will help you work out the rotation from the hero to the mouse
then you alter the _rotation property of the movieclip i.e.

this._rotation = Math.atan2
// not ---

oh wait. I found the code somebody helped me with ^_^

onClipEvent (enterFrame) {
Xd = _root._xmouse-_x;
//get distance from gun to mouse
Yd = _root._ymouse-_y;
// y posiion of mouse minus y position of gun
radAngle = Math.atan2(Yd, Xd);
//use atan2 to calculate angle?
_rotation = int((radAngle*360/(2*Math.PI))+90);
//rotate the gun to that same angle
}

obviously replace gun with hero but thatll get him facing the right direction.


The squirrel shall always triumph

BBS Signature
3dwarrior
3dwarrior
  • Member since: Nov. 3, 2003
  • Offline.
Forum Stats
Member
Level 10
Blank Slate
Response to Click, move to mouse 2007-06-09 20:03:08 Reply

At 6/9/07 07:26 PM, ChilliDog wrote: If it's so basic than let's see it.
And read the FIRST SENTENCE of my original post dumbass, I ALWAYS search before I post. There wasn't anything to learn from. Maybe you should learn to READ before you post...

This needs basic knowledge in order to create. Don't always expect everything that you want to do, to be already done by someone else. People learn by figuring things by themselves, not having others tell them how to do it.


AS2 Main | AS3 Main | Flash Wiki There is no excuse for not trying to learn. Linux.

BBS Signature
InfallibleSquirrel
InfallibleSquirrel
  • Member since: Jun. 22, 2006
  • Offline.
Forum Stats
Member
Level 10
Programmer
Response to Click, move to mouse 2007-06-09 20:05:47 Reply

At 6/9/07 08:03 PM, 3dwarrior wrote:
At 6/9/07 07:26 PM, ChilliDog wrote: If it's so basic than let's see it.
And read the FIRST SENTENCE of my original post dumbass, I ALWAYS search before I post. There wasn't anything to learn from. Maybe you should learn to READ before you post...
This needs basic knowledge in order to create. Don't always expect everything that you want to do, to be already done by someone else. People learn by figuring things by themselves, not having others tell them how to do it.

well actually i learnt half from tutorials and about half from asking for help on the NG forums so... yeah


The squirrel shall always triumph

BBS Signature
ChilliDog
ChilliDog
  • Member since: Feb. 16, 2007
  • Offline.
Forum Stats
Member
Level 16
Blank Slate
Response to Click, move to mouse 2007-06-09 20:12:19 Reply

At 6/9/07 08:03 PM, 3dwarrior wrote:
At 6/9/07 07:26 PM, ChilliDog wrote: If it's so basic than let's see it.
And read the FIRST SENTENCE of my original post dumbass, I ALWAYS search before I post. There wasn't anything to learn from. Maybe you should learn to READ before you post...
This needs basic knowledge in order to create. Don't always expect everything that you want to do, to be already done by someone else. People learn by figuring things by themselves, not having others tell them how to do it.

Why does everyone assume that I just want someone to make a game for me? Where did I ever say that? I'm asking because I WANT to learn, and for the 1242342345 time I DID TRY. I tried everything that I could before I asked, BOTH times. Posting on the BBS requires basic knowledge as well. Learn to read people, gahh.

Now, for those who actually helped, THANK YOU! It's very much appreciated.


AS: Main

Don't read this sentence.

noob-toast
noob-toast
  • Member since: May. 18, 2004
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
Response to Click, move to mouse 2007-06-09 20:29:30 Reply

active = p1;
onMouseDown = function () {
p1.hitTest(_xmouse, _ymouse, true) ? active=p1 : null;
p2.hitTest(_xmouse, _ymouse, true) ? active=p2 : null;
(!p1.hitTest(_xmouse, _ymouse, true) && !p1.hitTest(_xmouse, _ymouse, true)) ? loc={x:_xmouse, y:_ymouse} : null;
};
onEnterFrame = function () {
rot = Math.atan2(loc.y-active._y, loc.x-active._x)*180/Math.PI;
(active == p1 && !p1.hitTest(loc.x, loc.y, true)) ? (p1._x += Math.cos(rot*(Math.PI/180))*4, p1._y += Math.sin(rot*(Math.PI/180))*4, p1.gotoAndStop(2)) : p1.gotoAndStop(1);
(active == p2 && !p2.hitTest(loc.x, loc.y, true)) ? (p2._x += Math.cos(rot*(Math.PI/180))*4, p2._y += Math.sin(rot*(Math.PI/180))*4, p2._rotation=rot, p2.gotoAndStop(2)) : p2.gotoAndStop(1);
};

.txt
.swf/php


These new signatures can suck on mah balls. My lolis don't fit in. Lol wut what are you guys still doing on NG, move on.

BBS Signature
ChilliDog
ChilliDog
  • Member since: Feb. 16, 2007
  • Offline.
Forum Stats
Member
Level 16
Blank Slate
Response to Click, move to mouse 2007-06-09 20:32:02 Reply

Can we just turn the mean off here?

Your swf actually helped, THANK YOU, that's all you needed to do.

And yes, I searched AS Main but it didn't help that I didn't know what I was looking for. I read some things that I thought would help and tried to apply what I already knew but obviously came up with nothing. I'm trying to learn just like you have, I'm using all the resources I've found online but you can't possibly tell me that you knew exactly what you were looking for and where to look when you were in my position, or that you went through and read AS Main completely just to find what you were looking for. Sometimes yes, it IS nice to have things at your convenience but no one can expect that all the time, and I certainly don't. There's a lot of people that we can learn from here on NG but we'll get nowhere because people always have to be jerks about it. Can't we once just happily accept to help someone, or guide them in the right direction to help themselves? I never expected someone to do everything for me. That takes the fun out of it. All I wanted was a little help, ANY kind of help. I was offered code. I graciously accepted it. I didn't completely understand it, being at the level that I am so I asked some questions, to see if things could work the way I wanted them. Not once did I say "Give me the code that does this." People need to stop assuming and actually read. And being a little nicer once in a while wouldn't hurt. Next time someone asks for help and you're going to post, try actually helping. Don't post just to say "figure it out yourself n00b." Even if that's how you feel, what's the point in saying it? It just pisses people off and starts stupid flame wars that lead to nothing. I think we as a people here on NG need to grow up.

Now, thanks to all that offered help, it's greatly appreciated.


AS: Main

Don't read this sentence.

noob-toast
noob-toast
  • Member since: May. 18, 2004
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
Response to Click, move to mouse 2007-06-09 20:43:07 Reply

That normally gets you shot.

http://www.newgrounds.com/bbs/topic.php?id=40 7926
http://www.newgrounds.com/bbs/topic.php?id=38 3913
http://www.newgrounds.com/bbs/topic.php?id=29 3660
http://www.newgrounds.com/bbs/topic.php?id=30 0056


These new signatures can suck on mah balls. My lolis don't fit in. Lol wut what are you guys still doing on NG, move on.

BBS Signature
ShirkDeio
ShirkDeio
  • Member since: Mar. 29, 2007
  • Offline.
Forum Stats
Member
Level 16
Programmer
Response to Click, move to mouse 2007-06-12 10:18:12 Reply

Sorry I took so long to get back to you. Here it has the code for stopping the animation when it reaches the point, and doing the _xscale thing. I'm on a different computer now, so I can't test it, but this should work.

char_mc.onLoad = function() {
the_x = 0;
the_y = 0;
};
char_mc.onMouseDown = function() {
the_x = _xmouse;
the_y = _ymouse;
};
char_mc.onEnterFrame = function() {
if(this._x < the_x) {
this._xscale = 100;
this._x += 5;
};
if(this._y < the_y) {
this._y += 5;
};
if(this._x > the_x) {
this._xscale = -100;
this._x -= 5;
};
if(this._y > the_y) {
this._y -= 5;
};
if(this._y == the_y && this._x == the_x) {
this.stop();
};
};


I am a servant of the Most High God
The Hanging Collab || The Legend of Newgrounds (Game) || Crystal Rays (Song)

ChilliDog
ChilliDog
  • Member since: Feb. 16, 2007
  • Offline.
Forum Stats
Member
Level 16
Blank Slate
Response to Click, move to mouse 2007-06-15 22:01:10 Reply

GAH! Awesome, thanks so much Shirk!
I had figured out everything but the code to make it stop.
This works perfectly, thanks so much!


AS: Main

Don't read this sentence.