-
Move object towards mouse click
Hello everyone!
I am in a study group at Aalborg University in Denmark, where we are currently working on a small Flash Computergame, nothing big, nothing fancy, just part of a small project.
I am currently working on putting together a small game map, a map where a character can walk around and talk to NPC's and such. We've just started to I am pretty much just working with the basics here. Just started learning AS3 too. I have created an Object, in this case a circle that gets drawn when I run my AS code, the circle is drawn in the midde of my stage. What I want, since the game is Isometric and moves in those isometric lines rather than straight up, down, left, right, I want to make a funktion where, if the mouse is clicked at a location where both the X and Y coordinate is less than where the object is, then the object moves to that place.
I hope you can follow me on this, otherwise, please do tell.
The code I have, is as follows at the moment:
Code:
var xNewPos:int;
var yNewPos:int;
var inTransit:Boolean = false;
var cirkel:circle = new circle();
cirkel.x = 400
cirkel.y = 300
addChild(cirkel);
function mouseClick(evt:MouseEvent):void {
xNewPos = evt.target.mouseX;
yNewPos = evt.target.mouseY;
inTransit = true;
//trace(xNewPos);
//trace(yNewPos);
function circleMove(evt:MouseEvent):void {
if(inTransit = true){
if(xNewPos < cirkel.x && yNewPos < cirkel.y){
cirkel.x = cirkel.x -=5;
cirkel.y = cirkel.y -=2.5;
}
}
}
-
What would you like help with, exactly?
Can you to add the functions to stage or thing that is to record the mouse coordinates?
PHP Code:
stage.addEventListener(MouseEvent.CLICK, mouseClick);
Or do you need help using the tween class?
-
Sorry, I forgot the two eventlisteners in the code, dunno how that happened :S
Code:
var xNewPos:int;
var yNewPos:int;
var inTransit:Boolean = false;
var cirkel:circle = new circle();
cirkel.x = 400
cirkel.y = 300
addChild(cirkel);
stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseClick);
stage.addEventListener(MouseEvent.MOUSE_DOWN, circleMove);
function mouseClick(evt:MouseEvent):void {
xNewPos = evt.target.mouseX;
yNewPos = evt.target.mouseY;
inTransit = true;
//trace(xNewPos);
//trace(yNewPos);
}
function circleMove(evt:MouseEvent):void {
if(inTransit = true){
if(xNewPos < cirkel.x && yNewPos < cirkel.y){
cirkel.x = cirkel.x -=5;
cirkel.y = cirkel.y -=2.5;
}
}
}
That is what it looks like ...
-
Could you please explain a little more clearly what you're having trouble with. The stuff below works, in the sense that if you click the area covered by the circle you nudge it a bit. What EXACTLY is your problem?
Code:
var xNewPos:int;
var yNewPos:int;
var inTransit:Boolean = false;
var cirkel:Sprite = new Sprite;
cirkel.graphics.beginFill(0xFFCC00);
cirkel.graphics.drawCircle(30, 30, 30);
cirkel.x = 400
cirkel.y = 300
addChild(cirkel);
stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseClick);
stage.addEventListener(MouseEvent.MOUSE_DOWN, circleMove);
function mouseClick(event:MouseEvent):void {
xNewPos = event.target.mouseX;
yNewPos = event.target.mouseY;
inTransit = true;
//trace(xNewPos);
//trace(yNewPos);
}
function circleMove(evt:MouseEvent):void {
if(inTransit = true){
if(xNewPos < cirkel.x && yNewPos < cirkel.y){
cirkel.x = cirkel.x -=5;
cirkel.y = cirkel.y -=2.5;
}
}
}
-
Hey Eggler
My problem is, that the object needs to keep moving towards the mouse, and then stop when it reaches the point where the mouse is clicked. I hope you understand
-
You could try this:
remove stage.addEventListener(MouseEvent.MOUSE_DOWN, circleMove); and put an ENTER_FRAME inside your mouseClick function. Then the circleMove function (which you need to change in bold) will keep firing until the circle reaches the mouse position like so:
Code:
function mouseClick(event:MouseEvent):void {
stage.addEventListener(Event.ENTER_FRAME, circleMove);
xNewPos = event.target.mouseX;
yNewPos = event.target.mouseY;
inTransit = true;
//trace(xNewPos);
//trace(yNewPos);
function circleMove(evt:Event):void {
if(inTransit = true){
if(xNewPos < cirkel.x && yNewPos < cirkel.y){
cirkel.x = cirkel.x -=5;
cirkel.y = cirkel.y -=2.5;
}
}
}
I hope this helps.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|