ForumsProgramming ForumTUTORIAL: Your Second Forum game (Learn the not so basic)

4 3739
Secretmapper
offline
Secretmapper
1,747 posts
Nomad

Inspired by DaddydaNinja's Tutorial.

This is a simple top-to-bottom stealth game in which you will avoid nasty guards

Make a movieclip, design it as a ball.
Make another, Design it as little boxes, something like a map.

Assign the ball this movieclip

onClipEvent (load) {
power = 3;
radius = 6;
}
onClipEvent (enterFrame) {
if (Key.isDown(Key.LEFT)) {
_x -= power;
}
if (Key.isDown(Key.RIGHT)) {
_x += power;
}
if (Key.isDown(Key.UP)) {
_y -= power;
}
if (Key.isDown(Key.DOWN)) {
_y += power;
}
while (_root.wall.hitTest(_x, _y+radius, true)) {
_y--;
}
while (_root.wall.hitTest(_x, _y-radius, true)) {
_y++;
}
while (_root.wall.hitTest(_x-radius, _y, true)) {
_x++;
}
while (_root.wall.hitTest(_x+radius, _y, true)) {
_x--;
}
}

If you know actionscript 2.0, then you will probably know what I instanced the little boxes.

Make another movieclip, instance as COP
Make another one again, instance as line

Assign the cop this:
onClipEvent (enterFrame) {
dist_x = _root.hero._x-_x;
dist_y = _root.hero._y-_y;
dist = Math.sqrt(dist_x*dist_x+dist_y*dist_y);
angle = Math.atan(dist_y/dist_x)/(Math.PI/180);
if (dist_x<0) {
angle += 180;
}
if (dist_x>=0 && dist_y<0) {
angle += 360;
}
wall_collision = 0;
for (x=1; x<=dist; x++) {
point_x = _x+x*Math.cos(angle*Math.PI/180);
point_y = _y+x*Math.sin(angle*Math.PI/180);
if (_root.wall.hitTest(point_x, point_y, true)) {
wall_collision = 100;
break;
}
}
_root.line._x = _x;
_root.line._y = _y;
_root.line._rotation = angle;
_root.line._alpha = 100-wall_collision;
}

I didn't use hit.test because you might code other stuff like a mirror or something.

Now try it.

Of course you would need more things to make it exciting. This is just a tutorial

  • 4 Replies
manny6574
offline
manny6574
922 posts
Nomad

It's dannydaninja not daddydaninja!

Secretmapper
offline
Secretmapper
1,747 posts
Nomad

Oh.. yeah sorry 'bout that

Elitemagical
offline
Elitemagical
1,207 posts
Nomad

Excuse my ignorance - I've never touched AS in my life - I don't really understand this bit:

while (_root.wall.hitTest(_x, _y+radius, true)) {
_y--;
}


It's a while loop - but what exactly is it doing? If the y co-ordinate of the object + the radius is returns true then decrement the y co-ordinate? What does _root.wall.hitTest do and why are you adding the x co-ordinate into the condition?

Secondly, supposedly you don't need anything to notate variables like the one you defined, radius, so how comes the x co-ordinate and y co-ordinate require notation (an underscore)? Are they just special, pre-defined variables that require notation, or are there other cases where variables need to be notated using an underscore?
manny6574
offline
manny6574
922 posts
Nomad

I've never touched AS in my life


If you want to learn AS then learn as3. AS for the loop, I don't get the hitTest, in AS3 you got 2 types, making it easier: hitTestObject() and hitTestPoint(). I don't know how the old one works
Showing 1-4 of 4