Question

So I am making a game on AS2.0 where you control a square you can move and you are supposed not to touch any of the walls or else the level resets andyou go back to the initial position. I've managed to do it, but I have to make an If for each wall and it would be an eternal work when I reach the bigger levels, even if I copied/pasted. Is there any way of hittesting several objects at the same time? Thank-you If you need it, here's my code :D

on(keyPress "<Left>") {
    this._x -= 5;
}
on(keyPress "<Right>") {
    this._x += 5;
}
on(keyPress "<Down>") {
    this._y += 5;
}

on(keyPress "<Up>") {
    this._y -= 5;
}
onClipEvent(EnterFrame) {
    if (_root.square.hitTest(_root.wall)) {
        _root.touch._Alpha = 100;
        this._x = _root.x0;
        this._y = _root.y0;
    }
    if (_root.square.hitTest(_root.wall1)) {
        _root.touch._Alpha = 100;
        this._x = _root.x0;
        this._y = _root.y0;
    }
    if (_root.square.hitTest(_root.wall2)) {
        _root.touch._Alpha = 100;
        this._x = _root.x0;
        this._y = _root.y0;
    }
    if (_root.square.hitTest(_root.goal)) {
        _root.gotoAndStop(3);
    }
}

it is made on the Square's actions.

Was it helpful?

Solution

If you're ever copying and pasting large amounts of code with very slight differences, chances are high that you can shorten it and put it in to a loop (copying and pasting code repetitively can be a warning sign that something is wrong =b).

No, you can't check for it all at the "Exact" same time, but you can use a loop to solve your if statement problem. Stick all your wall movieclips in to an array (or something loopable) then loop through it.

An example of a loop version of your above code would be like:

// create an array and add a reference to all your wall clips
var collidableWalls:Array = new Array();
collidableClips.push(_root.wall);
collidableClips.push(_root.wall1;
collidableClips.push(_root.wall2);

Then after the array is made, you can check for hitTest for each wall by going:

for(var counter:Number = 0; counter < collidableClips.length; counter++){
    if (_root.square.hitTest(collidableClips[counter])) {
        _root.touch._Alpha = 100;
        this._x = _root.x0;
        this._y = _root.y0;
    }
}

For the moment, you would still need to handle the "_root.goal" separately as it has a different reaction.

if (_root.square.hitTest(_root.goal)) {
    _root.gotoAndStop(3);
}

Do not make the array in onClipEvent (EnterFrame) method, as it would cause the array to be made every single frame, resulting in a bit of overhead. Better to put it somewhere so that it is created only once (see advice). Also, it is also possible to dynamically reference movieclips by going:

_root["wall" + 1]

this would be the same as

_root.wall1

with this in mind, if all your wall clips are named the same (with a prefix "wall" and a number), you could do something like:

for(var counter:Number = 1; counter < 3; counter++){
    collidableClips.push(_root["wall" + counter]);
}

Overkill for just adding 2 walls, but if you have a few hundred of them, then it can be helpful (you could also skip using an array and compare directly).

Now some (decidedly opinionated) advice: I would move away from using " onClipEvent" and putting code on moveiclips asap. It can make your code very difficult to organise later on if your code is all over the place. Also you may run in to refernce issues and be confused as to which clips your code is on exactly (especially when it comes time to debug and you have many of them). I would suggest moving to 'frame scripts' instead (which I see is the 'next step').

Later, where possible, it would be good to avoid the use of _root. The habit of relying on it can cause a lot of issues down the road (such as if you start loading swfs in to one another, or moving to AS3, where _root does not exist at all).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top