-
Senior Member
A.S. explanation
Can someone please explain this piece of A.S. to a new flash user.
Please make it simple.
ThankZ
Code:
hero = _root.character;
s = 5;
b = _root.character.getBounds(_root.character);
function move(x, y) {
if (!_root.walls.hitTest(hero._x+x+b.xmin+1, hero._y+y+b.ymin+1, true)) {
if (!_root.walls.hitTest(hero._x+x+b.xmax-1, hero._y+y+b.ymin+1, true)) {
if (!_root.walls.hitTest(hero._x+x+b.xmin+1, hero._y+y+b.ymax-1, true)) {
if (!_root.walls.hitTest(hero._x+x+b.xmax-1, hero._y+y+b.ymax-1, true)) {
hero._x += x;
hero._y += y;
}
}
}
}
}
if (Key.isDown(Key.UP)) {
_root.hero.play();
move(0, -s);
_root.hero._rotation = 90;
}
if (Key.isDown(Key.DOWN)) {
_root.hero.play();
move(0, s);
_root.hero._rotation =-90;
}
if (Key.isDown(Key.LEFT)) {
_root.hero.play();
move(-s, 0);
_root.hero._rotation =0;
}
if (Key.isDown(Key.RIGHT)) {
_root.hero.play();
move(s, 0);
_root.hero._rotation = 180;
}
if (Key.isDown(Key.LEFT)&& (Key.isDown(Key.UP))){
_root.hero._rotation = 40;
}
if (Key.isDown(Key.RIGHT)&& (Key.isDown(Key.UP))){
_root.hero._rotation = 140;
}
if (Key.isDown(Key.LEFT)&& (Key.isDown(Key.DOWN))){
_root.hero._rotation = -40;
}
if (Key.isDown(Key.RIGHT)&& (Key.isDown(Key.DOWN))){
_root.hero._rotation = -140;
}
-
Senior Member
code:
// _root.character is a movieclip that corresponds to a character walking thru
// the a game (perhaps a maze or dungeon)
// here we assign it to the variable hero
hero = _root.character;
// S is the speed of movement - 5 pixels per jump
s = 5;
// The getBounds() method returns an object containing the variables xmin,xmax,ymin and ymax -
// these contain the coordinates of the bounding box of the character, which
// will be used for collision testing below
b = _root.character.getBounds(_root.character);
// This function moves the character if it isn't bumping into any walls
// it performs a hit test with each of the 4 corners of the character
// using the values returned by getBounds().
// If each of those 4 individual hittests return false, then we move the character in the x,y direction.
// You may be wondering why a single hitTest (such as _root.walls.hitTest(hero))
// is not used - this is because that kind of hittest will always return TRUE
// if the bounding box of the walls and the hero intersect.
// the point hittests used here are more precise, and only return true if the visible pixels in the walls intersect the points tested.
function move(x, y) {
if (!_root.walls.hitTest(hero._x+x+b.xmin+1, hero._y+y+b.ymin+1, true)) {
if (!_root.walls.hitTest(hero._x+x+b.xmax-1, hero._y+y+b.ymin+1, true)) {
if (!_root.walls.hitTest(hero._x+x+b.xmin+1, hero._y+y+b.ymax-1, true)) {
if (!_root.walls.hitTest(hero._x+x+b.xmax-1, hero._y+y+b.ymax-1, true)) {
hero._x += x;
hero._y += y;
}
}
}
}
}
// These bits of code respond to keypresses
// The hero character probably has a movement animation
// when a movement key is pressed, we trigger that animation and attempt to move in that direction.
// s is the speed of movement.
// the rotation is modified to point to]
// point the character in the direction of movement
// this kind of rotation is typically done in a game with an overhead view
if (Key.isDown(Key.UP)) {
_root.hero.play();
move(0, -s);
_root.hero._rotation = 90;
}
if (Key.isDown(Key.DOWN)) {
_root.hero.play();
move(0, s);
_root.hero._rotation =-90;
}
if (Key.isDown(Key.LEFT)) {
_root.hero.play();
move(-s, 0);
_root.hero._rotation =0;
}
if (Key.isDown(Key.RIGHT)) {
_root.hero.play();
move(s, 0);
_root.hero._rotation = 180;
}
// This code handles rotations for diagonal movements
if (Key.isDown(Key.LEFT)&& (Key.isDown(Key.UP))){
_root.hero._rotation = 40;
}
if (Key.isDown(Key.RIGHT)&& (Key.isDown(Key.UP))){
_root.hero._rotation = 140;
}
if (Key.isDown(Key.LEFT)&& (Key.isDown(Key.DOWN))){
_root.hero._rotation = -40;
}
if (Key.isDown(Key.RIGHT)&& (Key.isDown(Key.DOWN))){
_root.hero._rotation = -140;
}
-
Senior Member
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|