«

»

Sep 01

Game Programming (swishmax 4) – Tank Movement – advanced

Click on the movie to activate it
Use the arrow keys to move the tank and the z and x keys to rotate the turret.
Example:

Base.png

turret.png

Step.1
save the images above, import the base.png into swishmax 4, group it as a movieclip and name it base.
import the turret.png and group it as a movieclip, name it as turret, center the transform point for both images.
now group both the movieclips together as a new movieclip and name it tank.
add the following script to the tank movieclip.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
onSelfEvent (enterFrame) {
    if (Key.isDown(Key.UP)) {
        speed = 3;
    }else{
        speed = 0
    }
    if (Key.isDown(Key.DOWN)) {
        speed = -3;
    }
    if (Key.isDown(Key.LEFT)) {
        _rotation -= 2;
    }
    if (Key.isDown(Key.RIGHT)) {
        _rotation += 2;
    }
    if (Key.isDown(Key.Z)) {
        turret._rotation-=2;
    }
    if (Key.isDown(Key.x)) {
        turret._rotation+=2;
    }
    speed *= .98;
    x = Math.sin(_rotation*(Math.PI/180))*speed;
    y = Math.cos(_rotation*(Math.PI/180))*speed*-1;
        _x += x;
        _y += y;
        //
        hi = Math.sin(_rotation * 1.745329E-002) * 30;
    ip = Math.cos(_rotation * 1.745329E-002) * 30;
    _root.k1 = hi;
    _root.k2 = ip;
    if (Key.isDown(Key.UP)) {
        _root.drawing.lineStyle(6, 000000, 1.5);
        // line thickness, rgb, alpha
    }
    else {
        _root.drawing.lineStyle(6, 000000, 1.5);
        // line thickness, rgb, alpha
    }
    _root.drawing.moveTo(iks, ips);
    _root.drawing.lineTo(_x - ip, _y - hi);
    _root.drawing.moveTo(iks2, ips2);
    _root.drawing.lineTo(_x + ip, _y + hi);
    iks = _x - ip;
    ips = _y - hi;
    iks2 = _x + ip;
    ips2 = _y + hi;
}
onSelfEvent (load) {
     _root.createEmptyMovieClip("drawing", 1);
    this.swapDepths(_root.drawing);
    _root.clearBtn.swapDepths(this);
    hi = Math.sin(_rotation * 1.745329E-002) * 30;
    ip = Math.cos(_rotation * 1.745329E-002) * 30;
    iks = _x - ip;
    ips = _y - hi;
    iks2 = _x + ip;
    ips2 = _y + hi;
}


VN:F [1.9.22_1171]
Rating: 5.0/5 (1 vote cast)
VN:F [1.9.22_1171]
Rating: +1 (from 1 vote)
Game Programming (swishmax 4) - Tank Movement - advanced, 5.0 out of 5 based on 1 rating

2 comments

  1. pitrsman

    Excelent base for vehicle movement, thank you. Just one thing, please fix backward movement: speed = 3, to -3 and rename movieclip turret to gun and then is everything working perfect. Sorry for this. Bye

    VN:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VN:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
  2. Craig Lowe

    Thanks for the heads up, all amended :-) .

    VN:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VN:F [1.9.22_1171]
    Rating: 0 (from 0 votes)

Leave a Reply

9 visitors online now
4 guests, 4 bots, 1 members
Max visitors today: 11 at 02:48 am UTC
This month: 27 at 05-17-2013 01:24 pm UTC
This year: 39 at 01-01-2013 12:46 pm UTC
All time: 109 at 12-07-2012 11:00 pm UTC