Skip to main content
September 24, 2015
Answered

i have a character that is 64x64 pixels that i want to move along the x,y axis by clicking on a spot with the mouse. the character should move in increments of 64 pixels.

  • September 24, 2015
  • 2 replies
  • 766 views

i have a character that is 64x64 pixels that i want to move along the x,y axis by clicking on a spot with the mouse. 

the character should move in increments of 64 pixels.

Here is what i have.

// Mouse Controls
var iswalking = false; // is the character moving?
var goX = F1MC.x; // goX - the point on the X axis to which char must move
var goY = F1MC.y; // same as goX but on the Y axis

trace(goX)
trace("Char Start X");
trace(goY)
trace("Char Start Y");
var dir = "down"; // current direction of movement
stage.addEventListener(MouseEvent.CLICK, setposition);

// set the position to which char must move on click

function setposition(MouseEvent)
{
goX = mouseX
goY = mouseY

trace(goX)
trace("Mouse Click Position X");
trace(goY)
trace("Mouse Click Position Y");
}

The part below is in the Game Loop


// Mouse Click Movements
if(iswalking == false)
{
if(dir == "Down")
{
F1MC.gotoAndStop("F1 Stand Down Frame");
}
else if(dir == "Up")
{
F1MC.gotoAndStop("F1 Stand Up Frame");
}
else if(dir == "Left")
{
F1MC.gotoAndStop("F1 Stand Left Frame");
}
else if(dir == "Right")
{
F1MC.gotoAndStop("F1 Stand Right Frame");
}
}

// Move Down
if((goY-WalkSpeed) > F1MC.y)
{
F1MC.y += WalkSpeed;
F1MC.gotoAndStop("F1 Walk Down Frame");
dir = "Down";
}
// Move Up
else if((goY+WalkSpeed) < F1MC.y)
{
F1MC.y -= WalkSpeed;
F1MC.gotoAndStop("F1 Walk Up Frame");
dir = "Up";
}
// Move Right
else if((goX-WalkSpeed) > F1MC.x)
{
F1MC.x += WalkSpeed;
F1MC.gotoAndStop("F1 Walk Right Frame");
dir = "Right";
}
// Move Left
else if((goX+WalkSpeed) < F1MC.x)
{
F1MC.x -= WalkSpeed;
F1MC.gotoAndStop("F1 Walk Left Frame");
dir = "Left";
}
else
{
iswalking = false;
}

This topic has been closed for replies.
Correct answer

Never mind solved the problem.  It wasn't the code. I somehow managed to shift the sprite up and left 1 pixel in the scene.  since the origin point for the sprite was the top left corner that 1 pixel shift was enough to be considered 1 block up and 1 block left.  Not sure why it only affected if moving up or left. but re aligning the sprite has solved the issue.

2 replies

Participant
February 2, 2016

can i get your project?? only move character. I really need example for that...Thank you very much

Ned Murphy
Legend
September 24, 2015

What is failing to happen with the code you show?  I would expect that the object will never reach its destination due to not accounting for distances less than the WalkSpeed value, which you say is 64.  Where do you set that value?  How is this code implemented in what you are calling the game loop?

Correct answer
September 24, 2015

Never mind solved the problem.  It wasn't the code. I somehow managed to shift the sprite up and left 1 pixel in the scene.  since the origin point for the sprite was the top left corner that 1 pixel shift was enough to be considered 1 block up and 1 block left.  Not sure why it only affected if moving up or left. but re aligning the sprite has solved the issue.