View Full Version : Selecting one of several MCs and move independently of others
Sapster
February 18th, 2009, 03:08 AM
Thanks to help from members on another thread (here (http://kirupa.com/forum/showthread.php?t=320017)) I have a script that enables me to face and move a single MC using arrow keys, which will continue to move in the same direction at a constant rate after a key is pressed.
What I'm looking to do now is exactly the same thing but with multiple MCs on screen (say 2+), but be able to 'select' each one independently of the others with the Mouse and use the same direction keys (or buttons) to control them in the same manner without affecting the direction or movement of the other(s).
Example: 2 objects. Mouse click on one object and use an arrow key or button to send it off in a given direction. Then, mouse click on another object and send it off in an independent direction, using the same keys/buttons without affecting the first object's current motion.
Can anyone give me an idea of what I would need to do to accomplish this? I've found plenty of tutorials which talk about creating MC buttons, but not sure how to proceed from there.
Thanks in advance for any help.
cjke.7777
February 18th, 2009, 05:34 AM
Hey mate I think I can help. Using the code you got from Charleh I have modified it to control multiple mc's as you need.
First take the actionscript out of the mc and move it to its own layer and name this layer "actions". This should layer should be on the main timeline.
I am going to call the instances of your movieclips snake1 and snake2. So you should have two movieclips on the stage named snake1 and snake2. These two "snakes" should be on a separate layer to the actions layer.
var speed:Number = 0.5; // Assume constant for now
var dir:Number = 1; // The direction of movement
var keyListener:Object = new Object;
var selectedSnake:MovieClip = snake1;
Key.addListener(keyListener);
keyListener.onKeyDown = function() {
switch(chr(Key.getAscii())) {
case Key.RIGHT:
this.gotoAndStop(2);
dir = 2;
break;
case Key.LEFT:
this.gotoAndStop(4);
dir = 0;
break;
case Key.UP:
this.gotoAndStop(1);
dir = 1;
break;
case Key.DOWN:
this.gotoAndStop(3);
dir = 3;
} // end switch
}
this.onEnterFrame = function() {
switch(dir) {
case 0:
selectedSnake._x -= speed;
break;
case 1:
selectedSnake._y -= speed;
break;
case 2:
selectedSnake._x += speed;
break;
case 3:
selectedSnake._y += speed;
break;
}
}
snake1.onRelease = snake2.onRelease = function() {
selectedSnake = this;
}
Hope this helps - btw i'm currently uninstalling cs3 and installing cs4 (yay) and didn't actually test the code. I will test it in about 30 minutes to confirm it but the theory is correct.
Sapster
February 18th, 2009, 08:10 AM
G'day Cjke,
I gave it a quick go in CS4 without studying your code (will take a while for me to understand it, though). Not 100% sure that I've done everything correctly as per your instructions, but at the moment:
a) I'm not getting any direction/facing changes from keyboard input.
Edit- by changing line 9 to: switch(Key.getCode()) { I get the arrow keys to work for direction of movement but not the direction the object is facing. So, not sure if it's the right thing to do.
b) Upon loading, snake1 is moving up in dir 1, but snake2 is static (not really an issue about snake2 actually).
c) Both MCs are clickable. However, when I click on snake2, snake1 stops and snake2 will start moving upward in dir 1, and vice versa (this is a problem, as I need both MCs to be able to be moving at the same time and be able to change direction with key input after being selected, independently of what the other MC doing).
Edit: with the switch(Key.getCode()) { change above, after moving i.e. snake1 in a certain direction, if I click on snake2, snake1 still stops, and snake2 begins moving automatically in the same direction snake1 was moving.
Please let me know if you get a chance to test it yourself just so I know for sure if the error is on my side.
In any case, thank you very much for the help...it's very much appreciated. These forums are a fantastic tool. I'll study the code you've given me and see if I can nut it out.
Cheers.
cjke.7777
February 18th, 2009, 05:00 PM
Ok I didn't understand that you wanted both snakes moving all the time. Thats an easy fix - when I get home from work tonight I'll update it. Expect an answer later on.
cjke.7777
February 19th, 2009, 04:43 AM
Hi - Checked the code for you and refined it a bit. You can set the speeds of the snakes separately so as they eat (or whatever they do) you can adjust.
var speed1:Number = 1;// Assume constant for now
var speed2:Number = 1;// Assume constant for now
var dir1:Number = 2;// The direction of movement
var dir2:Number = 2;// The direction of movement
var keyListener:Object = new Object();
var selectedSnake:MovieClip = snake1;
Key.addListener(keyListener);
keyListener.onKeyDown = function() {
switch (Key.getCode()) {
case Key.RIGHT :
selectedSnake == snake1 ? dir1=0 : dir2=0;
selectedSnake._rotation = 90;
break;
case Key.LEFT :
selectedSnake == snake1 ? dir1=1 : dir2=1;
selectedSnake._rotation = -90;
break;
case Key.UP :
selectedSnake == snake1 ? dir1=2 : dir2=2;
selectedSnake._rotation = 0;
break;
case Key.DOWN :
selectedSnake == snake1 ? dir1=3 : dir2=3;
selectedSnake._rotation = 180;
break;
}// end switch
};
this.onEnterFrame = function() {
switch (dir1) {
case 0 :
snake1._x += speed1;
break;
case 1 :
snake1._x -= speed1;
break;
case 2 :
snake1._y -= speed1;
break;
case 3 :
snake1._y += speed1;
break;
}
switch (dir2) {
case 0 :
snake2._x += speed2;
break;
case 1 :
snake2._x -= speed2;
break;
case 2 :
snake2._y -= speed2;
break;
case 3 :
snake2._y += speed2;
break;
}
};
snake1.onRelease = snake2.onRelease=function () {
selectedSnake = this;
selSnake.gotoAndStop(this._name.substr(5,1));
};
See the following for the swf in action:
http://www.users.on.net/~cjke/swf_examples/snake/snake.html (http://www.users.on.net/%7Ecjke/swf_examples/snake/snake.html)
(remember to click to focus the view)
What do you think?
Sapster
February 19th, 2009, 05:53 AM
cjke,
Just got home from work and had a look at the swf. Haha! That's fantastic! Great work. Will give it a try in my CS4 and make sure I can make it work for myself. It's not actually for a snake game :) (more like a simulation of a simulation) but no doubt there will be some out there that will happily use it for Snake.
Thanks again. Huge help! I'd be going nowhere fast without the members in this forum :)
Cheers.
cjke.7777
February 19th, 2009, 06:16 AM
Cool not a problem - if you need more help let me know. Or more importantly if you want to know how the code works and also why i chose to code it that way just post and we'll discuss it.
I'm on CS4 now too - so you should be able to load it straight up. Other wise I can give you the fla file.
Sapster
February 19th, 2009, 06:28 AM
Actually, sorry to be a bother, but I'll take you up quickly on the offer of more help if that's cool. There's a lot yet to do that I really don't know how I'm going to tackle just yet, but right now I'm interested in the code for the color changes in that box when each snake is selected. I'm so new to this that I haven't even identified yet which part of the code that is! Is it in the code above?
What I'd like to do is make the MC itself change color when selected i.e. to white (both to same color when selected). Is that a major change?
Also, I'd definitely like to know more about how all that works, but probably best if I spend some time experimenting with it first.
Cheers.
cjke.7777
February 19th, 2009, 06:48 AM
The little box that changes color is just a movieclip with two frames. The name of that box is just selSnake.
this._name.substr(5,1) // gets the 1 or 2 from the string snake1 or snake2
so effectively the next line is saying:
selSnake.gotoAndStop(1); or selSnake.gotoAndStop(2);
To change the color of the mc's are easy enough. Actually you might do something similar, a frame for a selected state and a frame for an unselected state. Then use a line similar to above.
Did you need a .fla to show it working?
Sapster
February 19th, 2009, 06:55 AM
Ah, I think I understand! (in a vague kind of way).
Lemme have a play around with it and see what I can come up with. Will shout if the head vs wall thing gets too painful!
Thanks yet again.
cjke.7777
February 19th, 2009, 05:04 PM
Ok just let me know
Sapster
February 22nd, 2009, 06:41 AM
Hey cjke,
My head's getting a little tender... I've gone over the code and I'm still having some trouble with the color change side of things with respect to the MCs.
To be more specific, I'm planning to have up to 15 MCs on the stage at any given time, starting with an initial 5 or so, with another, say 30-40 appearing randomly over a period of time. That'll be a headache for another time I guess... but my concern is that this will impact on the best method used for the color change.
The 5 or so initial MCs will initially be a given color i.e. green. When selected to adjust their movement, they will turn a different color i.e. white, to identify them as the currently selected MC. When another MC is selected, the previously selected MC will return to the original color, and the newly selected MC will show as white.
Does this requirement and the number of MCs I'm considering impact on the method you had in mind?
cjke.7777
February 22nd, 2009, 05:22 PM
In this care you may want to apply a tint adjustment to the MC's or something similar. That way you can randomly add any color to the MC's, regardness the number of them on the screen.
It would be simple for them to swap to the Selected White color then back to the original, basically you would change the data at the top of the as file to be a class in a seperate file something similar to this:
public class Snake extends MovieClip {
private var speed:Number;
private var color:uint;
private var dir:int;
public function Snake(theSpeed:Number, theColor:uint, theDir:int){
speed = theSpeed;
color = theColor;
dir = theDir;
}
// a bunch of set/get methods should follow this to set the above private variables
}
Then in your main actionscript create an array of type Snake.
var snakes:Array = [];
Then when ever you add a snake (or whatever) you do something like this:
var theSnake:Snake = new Snake(2, 0x445500, 2); // sets the speed, color and dir of new snake
snakes.push(theSnake); // adds this new snake to the array
// later in script you can check the speed, color or dir of any snkae in the array by ...
trace(snakes[4].color); // will the color of the 5th snake in the array
does this help at all?
Sapster
February 22nd, 2009, 11:07 PM
Thanks for the info, cj. Will need to take some time to look at this.
Cheers.
cjke.7777
February 23rd, 2009, 03:01 AM
sure if you have questions just ask
cj
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.