Moveing with KEYS

hi guys,
i have tow quesation…?
first one :
i’m working in big map… i make a buttons to move it right, left, up and down. it works great. tow buttons for zoom in and out and it works too…
i need to make it move with 45 degree when i press up and right… how can i do it, but i don’t want to rotate it.

second:
how can i make zoom with selection… i mean when i press mouse and draw rectangle in somewhere it makes zoom for this area.

thanx

nobody can HELP me… :frowning:

Concerning your first question, it depends on your code, really. When I have to do things like that, I usually use temporary variables to store the position change. Something like that:

function listenForKeys(){
  if (Key.isDown(Key.LEFT)) yourClip.x=1;
  if (Key.isDown(Key.RIGHT)) yourClip.x=-1;
  if (Key.isDown(Key.UP)) yourClip.y=1;
  if (Key.isDown(Key.DOWN)) yourClip.y=-1;
}

function updatePosition(){
  yourClip._x+=yourClip.x;
  yourClip._y+=yourClip.y;
}

this.onEnterFrame=function(){
  listenForKeys();
  updatePosition();
}

Concerning the second, I have no idea :trout:

what about tow keys in the same times… (-:
i mean when you play game and want to move you ship to 45 drgree… so you will press Right and UP… :nerd:

How can i do it?
Concerning this in the first … :slight_smile:

thanx

That case is handled, Flashswimmer. Look at the listenForKeys function :slight_smile: But there will be a problem if you press left AND right for instance…

Can’t you just do…

[AS]if(Key.isDown(Key.RIGHT) && Key.isDown(Key.UP)) {
yourClip.x=1;
yourClip.y=1;
}[/AS]

??

I tried this, and it seemed to work…
[AS]speed = 1;
moveClip = new Object();
moveClip.onKeyDown = function() {
if (Key.isDown(Key.LEFT)) {
yourClip._x += speed;
}
if (Key.isDown(Key.RIGHT)) {
yourClip._x += -speed;
}
if (Key.isDown(Key.UP)) {
yourClip._y += speed;
}
if (Key.isDown(Key.DOWN)) {
yourClip._y += -speed;
}
};
Key.addListener(moveClip);[/AS]

Why make something simple when you can obfuscate it? :sure:

No, actually, what I wrote can (could?) be useful if you had to test the position to be sure you’re not out of bounds. Man, I just can’t explain myself today :crazy: :stuck_out_tongue:

[AS]moveClip = new Object();
moveClip.onKeyDown = function() {
Key.isDown(Key.LEFT) ? yourClip._x += speed : null;
Key.isDown(Key.RIGHT) ? yourClip._x += -speed : null;
Key.isDown(Key.UP) ? yourClip._y += speed : null;
Key.isDown(Key.DOWN) ? yourClip._y += -speed : null;
};
Key.addListener(moveClip);[/AS]

:stuck_out_tongue:

Yeah, your method could be useful in some situations indeed, I wasn’t putting your method down, just offering alternative solutions.

Ok, how about with a little easing?

[AS]easeSpeed = 4;
slideSpeed = 10;
MovieClip.prototype.easeTo = function(xDir, yDir) {
endX = this._x+xDir;
endY = this._y+yDir;
this.onEnterFrame = function() {
this._x += (endX-this._x)/easeSpeed;
this._y += (endY-this._y)/easeSpeed;
};
};
_root.onEnterFrame = function() {
Key.isDown(Key.LEFT) ? yourClip.easeTo(slideSpeed, 0) : null;
Key.isDown(Key.RIGHT) ? yourClip.easeTo(-slideSpeed, 0) : null;
Key.isDown(Key.UP) ? yourClip.easeTo(0, slideSpeed) : null;
Key.isDown(Key.DOWN) ? yourClip.easeTo(0, -slideSpeed) : null;
Key.isDown(Key.RIGHT) && Key.isDown(Key.UP) ? yourClip.easeTo(-slideSpeed, slideSpeed) : null;
Key.isDown(Key.LEFT) && Key.isDown(Key.UP) ? yourClip.easeTo(slideSpeed, slideSpeed) : null;
Key.isDown(Key.RIGHT) && Key.isDown(Key.DOWN) ? yourClip.easeTo(-slideSpeed, -slideSpeed) : null;
Key.isDown(Key.LEFT) && Key.isDown(Key.DOWN) ? yourClip.easeTo(slideSpeed, -slideSpeed) : null;
};[/AS]

Man… I am sooo bored. I gotta watch my dog today (he just had surgery yesterday) and all he is doing is sleeping!!! Which is normal, but still, sheesh, I need SOMETHING to do here.

How about that scripting battle? :stuck_out_tongue:

You can also check the levitated update :slight_smile:

Ermmm :-\

I dunno yet, I have NO clues as towhat to make, and my scripting ability with just AS isn’t too hot, especially when trying to keep it withing a certain amount of lines.

You know my code efficiency and optimization sucks :stuck_out_tongue:

Just checked Jared Tarbells new stuff. Almost wish I hadn’t, now I am just more jealous of him again… :hangover:

I like the walking critters thing, I always wanted to learn how to do that in AS. Any idea on where to learn? Or is it going to be one of those “try and see” type of deals?

You’re too modest, as always, and concerning levitated, it’s open source, so take a shovel and dig, Lost!!

LOL, it is Jared Tarbell though! His code is about as readable as Latin converted to Mandarin. Not to mention this particular experiment is OOP :hangover: <-- OOP does that to me.

But he does define everything into a bunch of tiny little functions, which makes it a bit easier to follow.

hi man… this is my code
[AS]
speed = 5;
_root.onEnterFrame = function() {
if (Key.isDown(Key.RIGHT)) {
_root.big_city._x += speed;
} else if (Key.isDown(Key.LEFT)) {
_root.big_city._x -= speed;
} else if (Key.isDown(Key.UP)) {
_root.big_city._y -= speed;
} else if (Key.isDown(Key.DOWN)) {
_root.big_city._y += speed;
}else if (Key.isDown(79)) {
_root.fullmap.map.text_all._visible =0
//_root.logo._visible =0
}else if (Key.isDown(67)) {
_root.fullmap.map.text_all._visible =1
//_root.logo._visible =1
}else if (Key.isDown(90)) {
_root.bag_city._xscale +=10
_root.bag_city._yscale +=10
}else if (Key.isDown(88)) {
_root.bag_city._xscale -=10
_root.bag_city._yscale -=10
}else if (Key.isDown(76)) {
_root.logo._visible =0
}else if (Key.isDown(71)) {
_root.logo._visible =1
}
};
[/AS]

it works good but when i tired you code (&&) i don’t know why it didn’t work.
i didn’t know how to do it with function… i thought about function but i failed… :frowning:
but now it works… thanx

Well so far thers a ton of different methods of this in this very thread.

But I think the problem here lies in the else if statements.

See, an else if says that if this is false then check this and if this is false then check the one after this. Now, when it finally reaches an if statement that is true, it stops running.

So unless you press both keys at the exact same time, the if statments will stop at the first button pressed and it won’t include the second button.

so that was my problem… :hangover:
i got the idea now lostinbeta… :slight_smile:
thanx to you and POM :blush:

Glad you got it fixed :slight_smile:

I added to my easing code to make it have easing zoom in capabilities too…

[AS]easeSpeed = 4;
slideSpeed = 10;
scaleBy = 10;
MovieClip.prototype.easeTo = function(xDir, yDir) {
endX = this._x+xDir;
endY = this._y+yDir;
this.onEnterFrame = function() {
this._x += (endX-this._x)/easeSpeed;
this._y += (endY-this._y)/easeSpeed;
};
};
MovieClip.prototype.scaleEase = function(newSize) {
mySize = this._xscale+newSize;
this.onEnterFrame = function() {
this._xscale = this._yscale += (mySize-this._xscale)/easeSpeed;
};
};
_root.onEnterFrame = function() {
Key.isDown(Key.LEFT) ? yourClip.easeTo(slideSpeed, 0) : null;
Key.isDown(Key.RIGHT) ? yourClip.easeTo(-slideSpeed, 0) : null;
Key.isDown(Key.UP) ? yourClip.easeTo(0, slideSpeed) : null;
Key.isDown(Key.DOWN) ? yourClip.easeTo(0, -slideSpeed) : null;
Key.isDown(Key.RIGHT) && Key.isDown(Key.UP) ? yourClip.easeTo(-slideSpeed, slideSpeed) : null;
Key.isDown(Key.LEFT) && Key.isDown(Key.UP) ? yourClip.easeTo(slideSpeed, slideSpeed) : null;
Key.isDown(Key.RIGHT) && Key.isDown(Key.DOWN) ? yourClip.easeTo(-slideSpeed, -slideSpeed) : null;
Key.isDown(Key.LEFT) && Key.isDown(Key.DOWN) ? yourClip.easeTo(slideSpeed, -slideSpeed) : null;
Key.isDown(90) ? yourClip.scaleEase(scaleBy) : null;
Key.isDown(88) ? yourClip.scaleEase(-scaleBy) : null;
};[/AS]

(this assumes your map is a perfect square though)