PDA

View Full Version : Flash Questions


backdoor2
04-25-2004, 10:51 PM
Can someone help me answer these questions please!! (In relation to Flash MX)

1. When using Flash’s attachMovieClip() method, what is the distinction of programming code inside versus outside of Symbols?
2. What is an example piece of code for instantiating multiple new Symbol objects at runtime with the necessary Layer and Name differentiation?
3. What is an example piece of code for continually detecting collision between two objects?
4. What is an example piece of code for using velocity variables to animate an object with keyboard input events handling acceleration?
5. What would be an effective method for enhancing Flash’s object collision to handle collision detection between intricately shaped objects utilizing their alpha channel as opposed to their rectangular boundaries?

emergency_pants
04-26-2004, 04:23 AM
hello.

1. Dunno. But I tend to put all my code either attached to symbols (movieClips) or on the timeline as frame scripts. I put less code inside symbols these days because as movies get more complex, it gets easier to lose track of where commands are being run from and what bit of code does what.

2. Here is a frame script which creates 20 clips on the stage, using attachMovie()

for (i=0; i<20; i++){
myClip.attachMovie( "circle", "circle"+i, 100+i );
}

this takes a clip called "circle" and attaches 20 instances of it to the clip called "myClip". The depths run from 100 to 120.

3. I would use a little enterFrame function:

_root.onEnterFrame = function(){
if (_root.box1.hitTest(_root.box2)){
trace("that's a hit!");
}
};

This code just tests for a hit between "box1" and "box2" on every frame.

4. Here is a code you can attach to a movie to give it motion using the keyboard. I think I found the original script here. Search through the tutorials here on actionscript.org and/or do a search at flashkit.com. There's loads of movement-based stuff on the web. Searches should throw up a lot of stuff for you.

// car movement
onClipEvent (load) {
maxSpeed = 15;
}
onClipEvent (enterFrame) {
// forward
if (Key.isDown(Key.UP)) {
speed += 1;
}
// reverse
if (Key.isDown(Key.DOWN)) {
speed -= 1;
}
// maximum speed
if (Math.abs(speed)>maxSpeed) {
speed *= .7;
}
// rotation
if (Key.isDown(Key.LEFT)) {
_rotation -= 15;
}
if (Key.isDown(Key.RIGHT)) {
_rotation += 15;
}
// brake
if (Key.isDown(Key.SPACE)) {
speed *= .9;
}
// movement calculation
speed *= .98;
x = Math.sin(_rotation*(Math.PI/180))*speed;
y = Math.cos(_rotation*(Math.PI/180))*speed*-1;
// hittest for the boundaries
if (!_root.boundary.hitTest(_x+x, _y+y, true)) {
_x += x;
_y += y;
} else {
speed *= -.6;
}
}


5. intricate shape against intricate shape is difficult. You can test point against intricate shape fairly easily.

if (_root.starShape.hitTest(_root._xmouse, _root.ymouse, true){
trace ("oh! HIT!");
}

It's in Flash help files. But I can't really tell you how to test two intricate shapes against each other.

I haven't placed the above e.gs into flash, so they're not tested, but I hope they help.

Any reasonable actionScript foundation book will show you how to do all these things. The friends of Ed series are pretty good and there are quite a few published by macromedia press. Perhaps someone can suggest some good titles.

It looks like you are writing games. i recently bought "flash MX2004 Game Design Demystified" and it's good for movement and collision testing.