[FMX] Controlling movement

I’m hoping this is a nice and simple question to answer, as I am not up too much, this is my first real thing I’ve made in Flash.

I want to have a scene I’ve created as the background to my header, and then 4 arrows, up, down, left, right - and then a “center” button.

I want to have the arrows scrolling the image around, and then if they click the center button to move it back to the center…

I have looked at this tutorial: http://www.kirupa.com/developer/actionscript/xymove.htm - but its not really what I am looking for, using the arrow keys that is.

Can anyone help?

Thanks in advance!

Tutorials teach theories of something, they just apply it in certain ways. It is up to you to show what you learned from the tutorial and modify it.

What you want to do is in fact in that tutorial, you just have to apply it to buttons instead of on keypresses :wink:

Amen. :stuck_out_tongue:

I have this sorted now, thanks…

I do still have one problem though, how do I limit how far it can move left and right, I have tried to find something about this, but cant… I dont want them to be able to move the image too far so it shows the blank background.

Thanks for your help, if you can help :slight_smile:

figure it out… little help:


"statement"? "action" :"else action"

That doesnt really help. Everyone used to be so helpful, you could just explain what I had to do without actually telling me, instead of talking in code =\

give me your .fla
i’ll “fix” it

Ember: You can use an if statement (fluid_one posted a method of the if statement, but a bit different).

Here is an example of how this would work…

[AS]var howFarRight = 550;
var howFarLeft = 0;
if (clip._x >= howFarRight){
clip._x = howFarRight;
} else if (clip_x <= howFarLeft){
clip._x = howFarLeft;
}[/AS]

(assumes your clips instance name is “clip”)
This says that if the clips x position is greater than or equal to the howFarRight variable, then it won’t be able to move any further because it will keep being placed at the howFarRight variable position.

The else if does the same thing, but checks the howFarLeft variable for moving left.

I understand now, is the how far left and right set from the center of the image, or from something else? I will work that out I guess.

Thanks for your help once again :slight_smile:

the howFarLeft and howFarRight variables are how far left and right you want to go on the stage.

So if your stage width is 550 and you want it to go all the way to the edge, then your howFarRight would be 550 (being 550 pixels to the right).

Now remember though, AS positions your clips according to its registration point, so if your clip has a centered registration point, then moving it all the way to the edge will take off that half of the clip.

What you could do is this though…

[AS]var howFarRight = 550-(clip._width/2);
var howFarLeft = clip._width/2;[/AS]

This obviously gets half of the width of the clip, and uses that to help decided how far this clip should go so that it doesn’t get cut off because of the registration clip position.

(note: above equations are for if your movie clip has a centered registration point)

What I want to do is, be able to move the image around the screen, without the right hand edge moving so far it reveals the background… and same with the left, so I think that will mean setting the howFarLeft/Right to the width of the image minus the width of the stage.

Ohhh, I gotcha, yeah, it does sound like the width of the stage minus the width of the image or something of that sort

Sorry, my brain isn’t on track right now, and I can’t picture it in my head clear enough to get an exact equation out of it that you can use.

But you have the idea of what you have to do down, so it shouldn’t be too hard after that :slight_smile:

post your fla - it’ll be much easier

But then they don’t learn how to do it themselves fluid. The point of helping to teach them how to do it so they know for future reference.

Just using code someone created from them only works if they study it and understand it fully.

Yeah, I’d prefer to work it all out myself, rather than posting it and having someone else do it. I am getting there. I’ll post it when I’m done :slight_smile:

Evidently I dont have it under control :frowning:

When you press the left key the image jumps off miles to left… same with up.

I have attatched the .fla - if someone wouldnt mind telling why its being so annoying…

Problem one…

lines like these…

[AS] var y;
y = y+2;
ud = _root.v-y;
_root.mover._y = ud;[/AS]

Can be simplified with just a simple…

[AS]_root.mover._y -= 2;[/AS]

(changing -= to += where necessary)
That reduces 4 lines to 1 line :wink:

Problem 2) You don’t have to define howFarLeft and howFarRight in each onKeyPress. Just do it in an onClipEvent(load)

[AS]onClipEvent (load) {
var howFarRight = 682;
var howFarLeft = 0;
var howFarUp = 0;
var howFarDown = 146;
}[/AS]

Problem 3) You don’t need to do if statements for howFarLeft AND howFarRight on each keypress, if the clip is moving right, you only need to check the howFarRight, and if you are moving it left, you only need to check the howFarLeft.

Problem 4) Somtimes you wrote mover_x instead of mover<B>.</B>_x (that dot makes a difference)… also, you don’t need to do mover._x, you can use this._x as well since the clip the code is on is the clip you are moving.

Problem 5) Flash’s _y axis is different, up is negative, and down is positive, so your <= and >= if statements were mixed up there.

Ive attached the fixed file. I commented a few things, but since its basically the same code being reused in slightly different ways I didn’t comment the whole thing.

Man I suck at this, lol… I think I’ll stick too HTML and PHP, hehe. Thanks for your help, I will look at the changes and learn from them… thank you!