1
votes

I'm new on here and needing a little help. Basically I'm designing a Pac-man like game which it has to be based on Wind in the Willows, so Mole-man it is!!

Anyway, I've done the basics of it, I've made my 2d mole, made him move, made him react to button presses etc. and now trying to get the walls to work, starting with the outer walls.

I made 4 rectangles around the edge, merged them to one shape and called it "outerWalls". I've been using hitTestObject to try and get it to work but with no success.

I did a test to make it work with moleman.xIncrement = -5 which makes him immediately move backwards without touching any walls. I am curious as to if this is because he is inside of the walls and it classes 4 outer walls as a square overall and as he is already inside the theoretical square moves him out? I am unsure. Have some code!!

Actions layer - frame 1:

import flash.events.KeyboardEvent;

var moleman = this.addChild(new Moleman_mc());

moleman.x = 100;
moleman.y = 200;
moleman.width = 24;
moleman.height = 24;

moleman.xIncrement = 5;
moleman.yIncrement = 0;

stage.addEventListener(KeyboardEvent.KEY_DOWN, doKeyDown);

function doKeyDown (e:KeyboardEvent):void
{
    switch(e.keyCode)
    {
        case Keyboard.UP:
        moleman.xIncrement = 0;
        moleman.yIncrement = -5;
        moleman.rotation = -90;
        break;

        case Keyboard.DOWN:
        moleman.xIncrement = 0;
        moleman.yIncrement = 5;
        moleman.rotation = 90;
        break; 

        case Keyboard.LEFT:
        moleman.xIncrement = -5;
        moleman.yIncrement = 0;
        moleman.rotation = 180;
        break;

        case Keyboard.RIGHT:
        moleman.xIncrement = 5;
        moleman.yIncrement = 0;
        moleman.rotation = 0;
        break;
    }
}

Actions layer - frame 2:

import flash.events.Event;

var collectCounter:int = 0;

moleman.x += moleman.xIncrement;
moleman.y += moleman.yIncrement;

if(moleman.hitTestObject(collect))
{
    collectCounter ++;
    collect.visible = false;
}

moleman.addEventListener(Event.ENTER_FRAME, wallHit);

function wallHit(event:Event):void
{
    if(moleman.hitTestObject(outerWalls))
    {
        moleman.stop();
    }
}

Actions layer - frame 3:

gotoAndPlay(2);

In addition, I cannot get the collect (e.g. collectables) to work, the plan would be to make it so each time you collect one "collect" item, that one disappears, you add one to collectCounter but all others stay visible. Currently if you collect one "collect" item, moleman just stops and stays where he is while another moleman appears and continues, not very useful. Any help on this would also be appreciated, not sure where to take it from there.

Any help is much appreciated, I am new at this but hoping it can go well :)

All help is much appreciated. John.

1
I would spend some time on google looking for tile based as3 games. While your approach may eventually work as you would like it, building it with the proper "building blocks" is the way to do something like this. Here is a tutorial for example - tbg.tonypa.pri.ee/start.html. Though the game play is a little different, the ideas are the same.sberry
@sberry That is a great tutorial, and I would recommend it to anyone interested in game development.Adam Harte
I would agree with @sberry in that your approach is not ideal. But this would be a good time to learn about how hitTestObject works. It's important to note that if you combine all those walls together, it's not just the walls that will register a hit - but anything inside that bounding box. Which, if I am understanding what you are saying, would mean that the WHOLE screen is within your bounding box. Here's a decent link for visually describing what I am talking about -- sierakowski.eu/list-of-tips/…prototypical
Thanks for the link :) I investigated it and build a new file upon it but end up with a million errors :/ I downloaded the provided source file to compare to but the only script it has in it is: import TBG07a; var mygame:TBG07a = new TBG07a(this); I'm unsure of this, where is all the other code hidden? 1 layer, 1 frame, nowhere else to look? Very confused. Also, can anybody explain to me what TGB07 (or other given number) is, I have to guess it's related to his tutorial names, but why would that be an import? Any help is appreciated, ta John.John McPherson

1 Answers

0
votes

In your case to detect collision you should use hitTestPoint method instead of hitTestObject. hitTestPoint method has the signature:

public function hitTestPoint(x:Number, y:Number, shapeFlag:Boolean = false):Boolean

so, when shapeFlag is set to true, it is possible to detect collision with the point (your hero) and maze shape (or any other arbitrary shape). This method is very powerful because the intersection with the real shape, not its bounding box is calculated. Modified wallHit method may look like this:

moleman.addEventListener(Event.ENTER_FRAME, wallHit);
function wallHit(event:Event):void
{

    if(outerWalls.hitTestPoint(moleman.x, moleman.y, true))
    {
      // collision
      moleman.x += -moleman.xIncrement;
      moleman.y += -moleman.yIncrement;
      moleman.xIncrement=0;
      moleman.yIncrement=0;
    }

    moleman.x += moleman.xIncrement;
    moleman.y += moleman.yIncrement;

if(moleman.hitTestObject(collect))
{
    collectCounter ++;
    collect.visible = false;
}

}

you may also remove frame 3 and "gotoAndPlay(2);" call because it is better to put the code which controls hero's position into ENTER_FRAME handler either.