PDA

View Full Version : RPG game development (need new hitTest code)


UknownXL
06-04-2008, 05:14 PM
Ok so I have been working on this rpg game I have created for a while and my hitTest isnt working properly. I have a main character named "hiro" and a movieclip called "Walls". "Walls" has many different movie clips in it and walls that your are not "suppose" to be able to walk over. It works okay for the most part but it has major bugs. Sometimes the "hiro" will be able to walk over part of the walls and get stuck. So what I would like is a more accurate hitTest code, and I was hoping someone herewould help me. Or atleast try and teach me.

An image of the game should be attached to this message.

Also here is the code im using.

onClipEvent (load) {
this._name = "hiro";
_root.RUN = 6;
}
onClipEvent (enterFrame) {
if (Key.isDown(Key.RIGHT)) {
if (_root.Walls.hitTest(getBounds(_root).xMax, _y, true)&&_root.hiroalive && !_root.hirohurt) {
_root.pushes = true;
} else {
if (_root.pause == false and _root.attack == false &&_root.hiroalive && !_root.hirohurt) {
this._x += _root.RUN;
_root.pushes = false;
}
}
} else if (Key.isDown(Key.LEFT)) {
if (_root.Walls.hitTest(getBounds(_root).xMin, _y, true) &&_root.hiroalive && !_root.hirohurt) {
_root.pushes = true;
} else {
if (_root.pause == false and _root.attack == false &&_root.hiroalive && !_root.hirohurt) {
_root.pushes = false;
this._x -= _root.RUN;
}
}
}
if (Key.isDown(Key.DOWN)) {
if (_root.Walls.hitTest(_x, getBounds(_root).yMax, true) &&_root.hiroalive && !_root.hirohurt) {
_root.pushes = true;
} else {
if (_root.pause == false and _root.attack == false &&_root.hiroalive && !_root.hirohurt) {
_root.pushes = false;
this._y += _root.RUN;
}
}
} else if (Key.isDown(Key.UP)) {
if (_root.Walls.hitTest(_x, getBounds(_root).yMin, true) &&_root.hiroalive && !_root.hirohurt) {
_root.pushes = true;
} else {
if (_root.pause == false and _root.attack == false &&_root.hiroalive && !_root.hirohurt) {
_root.pushes = false;
this._y -= _root.RUN;
}
}
}
}

NickZA
06-10-2008, 03:24 PM
Here are my suggestions to you (and probably the reasons you've not gotten any other responses so far):

1. Clean up your code. Believe me, part of the reason you're having problems is because YOU cannot read code that looks like that. Few people on here WANT to sort through poorly indented code, so they probably won't even bother reading your post. For beginner programmers (especially!) indent your code like this:

onClipEvent (load)
{
this._name = "hiro";
_root.RUN = 6;
}
onClipEvent (enterFrame)
{
if (Key.isDown(Key.RIGHT))
{
if (_root.Walls.hitTest(getBounds(_root).xMax, _y, true) && _root.hiroalive && !_root.hirohurt)
{
_root.pushes = true;
}
else if (_root.pause == false and _root.attack == false &&_root.hiroalive && !_root.hirohurt)
{
this._x += _root.RUN;
_root.pushes = false;
}
}
}


...etc.

2. Stop repeating yourself in your code, at least 50% of what you have written there is completely redundant. For example, you use this about 4(?) times in if statements:


if (_root.pause == false and _root.attack == false &&_root.hiroalive && !_root.hirohurt)


Why do you keep checking a huge set of conditions? Instead, use functions:

function canPush():Boolean
{
if (_root.pause == false and _root.attack == false &&_root.hiroalive && !_root.hirohurt) return = true;
}

onClipEvent(enterFrame)
{

...

_root.pushes = _root.canPush();

...

}


3. "and" / "&&" are the same thing. Choose one syntax and stick with it, don't mix and match as it makes your code less readable. "&&" is the way you would do better with, as it is the same across nearly all languages (eg. if you should ever begin to learn C).

4. Try to get out of using such an old-fashioned method of accepting events -- I am referring to: on(event) That is ancient syntax and will not serve you long in the world of Flash gaming if you ever want to be good at it. Furthermore this will make moving to ActionScript 3.0 that much harder for you if you decide to keep making games.

5. Stop using global (_root) variables for everything. This is incredibly bad organisation and makes it really difficult for you, or anyone else, to see how data is really organized in your code. What I am really saying here, is start using objects / classes ("OO Flash", Google it).

Finally:

Go and get yourself some decent sources on Flash game programming, so that you can see new, up-to-date code examples that will quickly show you how you should build things, and why you should do it that way. Also get some more general AS2 books (Colin Moock's Essential Actionscript for example) to help your general coding ability.

P.S. You don't actually need books, I learnt most (80%) of my ActionScripting from the web: sites like this one, Kirupa.com, FlashKit.com, GotoAndPlay.it, etc. They all have great tutorial sections.

I hope I haven't put you off. Keep trying, keep learning.

-Nick

UknownXL
06-11-2008, 07:53 AM
Dont worry you havent put me off. I have taught myself everything I know so im not surprised that im not doing it "the best way". Its hard to find teachers out there. No one will usually give me the time of day. But thank you for what you have said. I will try to take it to heart. Im still learning, so its no big deal.

NickZA
06-11-2008, 09:19 AM
Well, then you'll keep improving. :)

One of the things I said about event handlers, I just wanted to elaborate on (someone correct me if I'm wrong about this)...

The on(event) style you are using there tells me that you are putting your code on individual MovieClips. While this might be okay for your very first Flash game (while you are learning the basic ActionScript syntax), you should definitely steer clear of that style ASAP.

The reason is that, traditionally, languages that are used to make gamesdo not have a timeline as Flash does -- and this works in their favour, because controlling the flow of the game's logic takes place from one location -- the main game loop procedure (There is only one body of code). Flash complicates issues by giving you different/discrete/separate places where you can put code (i.e. on every different frame of every different movie clip). To make it easier for you to control the flow of your game, rather place all of your code into a single frame of the timeline (usually the first frame, or the second frame if you are going to use a preloader screen), and control everything from there. See this tutorial as an example:

http://www.emanueleferonato.com/2007/10/06/make-a-flash-game-like-flash-element-tower-defense-part-1/

(You will see that he says, just before the first section of code in that tutorial, that you should put everything in the first frame).

Try and pick apart tutorials like that and you'll begin to see the overall structure as it should be.

Again, good luck. Any questions, clarifications, just shout.

UknownXL
06-11-2008, 05:24 PM
I would still like to know a way of making a more accurate hitTest. This is actually my second game though. Here is my first game. http://www.notdoppler.com/breakout.php
Tell me what you think. Its not amazing, but I was suppose to do just the art. My programmer bailed on me so I coded it all myself.

NickZA
06-12-2008, 04:27 PM
Hey,

Your question is too broad in itself to have any single answer. The details you need to consider with collision detection (referred to as CD hereafter) are:

-Is your game vector or bitmap based?
-How much accuracy do you want in your collisions (i.e. must the collisions be exact)?
-If the answer to the above is "great accuracy" then you will need to also consider the performance hit that highly-effective CD will have on your game.

Usually, "fairly accurate" is good enough for most games.

I will post again later when I have time.

NickZA
06-13-2008, 02:04 PM
UPDATE: I have a few minutes, so...
With regards to deciding on the accuracy of your collision detections, you only need to do this if you are using bitmap graphics. Vector based graphics can do accurate collision detections by default. The Flash AVM can use the lines which make up a vector to do your accurate collision detection for you.
But if you're doing Bitmaps, here are the options:

1. Bounding box CD: What you're currently using. Not very accurate because it doesn't use the outline of your character as the collision detection area, instaed it uses the bounding box of the MC which your character's graphic is placed into. Implmentation Difficulty: Simple.

2. Circular/radius-based CD: define a spot at the centre of your sprite/mc, and detect if any part of another sprite/mc falls within that radius. Implmentation Difficulty: Easy to Medium (medium if you have many different sized objects rather than all sharing roughly the same size).

3. Pixel-perfect CD: Similar to using vectors, this is about as accurate as can be, only you get to use nicely drawn bitmaps into the deal. If you are going to go this route, you are pretty much going to have to convert all your displayable objects and rendering methods to use Flashes internal Bitmap/BitmapData classes. This is fairly advanced. For more on this, google "8bitrocket.com AS2 btimapdata". There are other ways to do pixel-perfect CD eg. using bitmasks, but in a way they are more trouble than they're worth, and slower than using BitmapData too.

4. Hybrid approach: Using a vector "CD mask" underneath a good-looking bitmap gives you the approximate effect of perfect CD. You use the mask for detecting collisions -- the bitmaps laid over them are only there for things to look good.


Any Q's once you've thought about this, let me know.