Platform shooter AI page 2

58 posts

Flag Post

Okay, I think I see now. Thanks, everyone.

How would I program something like feartehstickman drew? Sorry, I don’t know what to Google, and I’ve never done anything like this before.

 
Flag Post

One option is to hardcode a list of Point variables for each object (storing this in the relevant class as a static variable )

Another option is to make use of the functionality that the Flash IDE offers: create a node symbol (no need to export to ActionScript), graphically place the nodes around each MovieClip, give them instance names, and in code iterate through the nodes to work out their relative locations (see here )

The first solution is purely mathematical and is more efficient as it only stores information for the class rather than for each object of the class (as well as keeping things non-graphical)

An alternative solution which is very complex is to analyse the graphical image of the object itself and use a tolerance parameter to evaluate how to divide the shape into nodes

I’m sure others can come up with yet some more solutions

 
Flag Post

Thanks. Well, I would probably be using AS2; are point variables supported in AS2? If not, I could try the movieclip approach, but I would need more detail as this is all new to me. If someone’s bored and feels like making a simple example with step by step instructions, that would be appreciated.

I PM’d Jay Armstrong asking how he did the AI shooting in Combat Hero Adventures (AS2). There’s a good chance he’ll tell me a little about it; I’ve talked to him quite a bit before. Maybe I’ll just wait a bit and see if he responds; I’d probably use the method he used for his game.

 
Flag Post

If there’s no Point in AS2, then there is no issue with storing the x and y values separately.

Do you think you would be able to make your own Point class in AS2 if you had to? Half of the task is understanding how to approach the problem; then you have to put it into practice.

The MovieClip approach is something I came up with whilst thinking about solutions to a problem. If you want to go down that approach, then try to think about how to solve the problem using the tools you have available to you.

 
Flag Post

I want to learn how to program but it looks too difficult and complicated.

 
Flag Post
Originally posted by AllStarDominatio:

I want to learn how to program but it looks too difficult and complicated.

""You miss one-hundred percent of the shots you don’t take."
-Wayne Gretzkey"
-Michael Scott

 
Flag Post

The Point Class in AS3 only has 3 properties, and only a few methods (which you may or may not need all of). Making a custom version of it shouldn’t be too hard at all. That depends on how well AS2 cooperates with you though, which I’m not experienced enough to comment on.

 
Flag Post
Originally posted by Dealmaster13:

Do you think you would be able to make your own Point class in AS2 if you had to?

Maybe, but I’d have to use a movieclip for the point, right? I don’t see how I’d make a point class without a blank point movieclip.

I read the Point section in the AS3 documentation; how do you code a line connecting two points?

Thanks for the help.

I discovered a glitch in Combat Hero Adventures; right click isn’t disabled. If you press Play, for some reason the map graphics stay the same, but the background and map collision changes to a different map’s. The characters don’t hitTest the map graphics, they’re hitting something else. It could be points.

 
Flag Post

A Point class as we know it is a simple data structure, which at the very least contains two fields: numbers x and y

Define your Point class just as you would for any other class:


//package

class Point { //don't extend anything as you don't need any other class' properties to define a Point
  //declare your fields
  public var x:Number;
  public var y:Number;

  //define a constructor
  public function Point(x:Number, y:Number) {
    this.x = x;
    this.y = y;
  }
}

You can improve the implementation above further, in terms or security, functionality, or otherwise, should you wish.

For each boundary polygon node, of an object, at position (xi, yi), define a new Point: var point = new Point(xi, yi);
Perhaps store this in an array: var nodes:Array = new Array(); nodes.push(point);
Decide on your implementation strategy – perhaps each adjacent pair of nodes in nodes is an edge?
Perhaps create an edge class:


//package

//import Point

class Edge { //don't extend anything as you don't need any other class' properties to define an Edge
  //declare your fields
  public var startPoint:Point;
  public var endPoint:Point;

  //define a constructor
  public function Edge(startPoint:Point, endPoint:Point) {
    this.startPoint = startPoint;
    this.endPoint = endPoint;
  }

  //perhaps define an intersection function
  public function intersects(edge:Edge) : Boolean {
    //intersection algorithm
  }
}

var edges:Array = new Array(); edges.push(new Edge(nodes[i], nodes[i+1]));

It’s entirely up to you how you go about this.

If there was only one was of doing things while programming, it wouldn’t nearly be as entertaining as it is, and there wouldn’t be so much disparity between the capabilities of a ‘good’ and a ‘poor’ programmer.

 
Flag Post
Originally posted by GameBuilder15:
Originally posted by Dealmaster13:

Do you think you would be able to make your own Point class in AS2 if you had to?

Maybe, but I’d have to use a movieclip for the point, right? I don’t see how I’d make a point class without a blank point movieclip.

I read the Point section in the AS3 documentation; how do you code a line connecting two points?

Thanks for the help.

I discovered a glitch in Combat Hero Adventures; right click isn’t disabled. If you press Play, for some reason the map graphics stay the same, but the background and map collision changes to a different map’s. The characters don’t hitTest the map graphics, they’re hitting something else. It could be points.

The right-click>play is precisely one of the reasons you don’t use frames.

 
Flag Post

Thanks, Dealmaster. When I get a chance, I’ll give it a shot and let you know how it goes. The code you posted AS3 though. I know I’d need to add _’s in front of the x’s and y’s, but would the rest of it work okay? I’ve never made classes that don’t extend movieclips before.

RTL, you can just hide the right-click menu. I don’t think frames are bad. They can be a good way to keep things organized, in my opinion.

 
Flag Post
Originally posted by GameBuilder15:

Thanks, Dealmaster. When I get a chance, I’ll give it a shot and let you know how it goes. The code you posted AS3 though. I know I’d need to add _’s in front of the x’s and y’s, but would the rest of it work okay? I’ve never made classes that don’t extend movieclips before.

Unless AS2 is really strange, that should work perfectly. And you don’t need to put underscores in front of the x and y, because they are custom properties, not those of the MovieClip (unless AS2 needs underscores in front of every property?).
 
Flag Post

You don’t need underscores prepended to the x and y variables… think about why…

Might as well have defined the Point class as follows, but decided to be a little bit more conventional:


//package

class UselessClass { //don't extend anything as you don't need any other class' properties to define a UselessClass
  //declare your fields
  public var howAreYou:Number;
  public var helloImBob:Number;

  //define a constructor
  public function UselessClass(_482372:Number, ALPHA:Number) {
    howAreYou = _482372;
    helloImBob = ALPHA;
  }
}

var usefulPoint:UselessClass = new UselessClass(5, 10);

Perhaps you might want to Google what makes a valid variable name in AS2, and if you feel like it, compare what’s said for AS3, and hopefully you’ll be pleasantly surprised

It’s been a while since I’ve coded in AS2, and when I did, I wasn’t writing code like above, but you might be more surprised by the fact that I’m pretty sure the above is both valid and correct AS2 code and valid and correct AS3 code

It might be a good chance for you to reflect on having situations where you may want to define your own classes such as a Point or Edge class
Discover what you can achieve with classes

As a side note, being in Java mode, I was missing the function keyword from my methods, and translated all of the other accidental Java syntax to AS2

 
Flag Post

This is probably very basic, but what does it mean when you have something in the parenthesis (parameters?) of a function, like:


function Edge(startPoint:Point, endPoint:Point){

What does that do? I read the part in the documentation about it but I don’t understand.

 
Flag Post

Yeah, they’re function parameters

You call a function, passing in the relevant arguments: in this case, startPoint and endPoint (local variables).
The function can then manipulate the local variables.

Like Java, AS3 is call-by-value in the sense that all parameters are passed as values rather than references.
All primitive types (e.g. int, Boolean) are handled as raw values while all non-primitive types (e.g. Array, MovieClip, Point) are handled as references , which means that by passing a variable point of type Point to a function as a parameter, you are actually passing a reference to the variable point rather than a copy of point ’s binary data.
In the case of a primitive type parameter, due to the call-by-value nature of AS3, changing the primitive type within the function will not affect the original variable outside of the function. In the case of a non-primitive type parameter, due to the fact that these variables are handled as references, the reference to the variable is the same within and outside the function, so a change to the variable within the function will be visible outside the function and remain intact after returning from the function.

The snippet of code you’ve quoted above is a constructor for Edge.

http://www.tutorialspoint.com/java/java_methods.htm
http://www.oracle.com/technetwork/java/prog-140388.html#fields

 
Flag Post

Thanks. I carefully went over those articles for an hour or so, but I still don’t get what the purpose of parameters is. And honestly, I understood little. I bet a formal ActionScript course would help me a lot. I only know what I know from the internet, a couple books, trial and error.

What I’m getting at about the parameters is — what would happen if the parameters were left blank? Why are they needed? I know they’re arguments but I don’t see what they actually do.

 
Flag Post

The arguments are used inside the function. You’ll see the following 2 lines set the classes’ variables (startPoint, endPoint) using

this.startPoint = startPoint;
this.endPoint = endPoint;

Dealmaster provided a somewhat confusing example here. Since the class variables have the same name as the arguments, the example uses this.varname to make sure the first part is setting the class’s variable to the corresponding argument.

A better example might be:

package com.profusiongames.minipassage.menus
{
	public class MainMenu extends World
	{
		private var _currentLevel:String = "";
		public function load(levelName:String):void 
		{
			_currentLevel = levelName;
		}
	}
}

Where the argument passed in is stored more clearly in a class (instance) variable.

Now, why is this useful? Well, because I can call mainMenu.load("level1"); (where mainMenu is an instance of MainMenu) and store what level we are loading inside the MainMenu class. Then from somewhere else, I can check what our current level is.

EDIT In AS3, the underscores in a variable name usually indicate that a variable is private. Just a coding style that is quite popular. Makes it easy for variable names to not conflict and to provide easy identification of what variables you can access outside the class.

 
Flag Post
Originally posted by GameBuilder15:

What I’m getting at about the parameters is — what would happen if the parameters were left blank? Why are they needed? I know they’re arguments but I don’t see what they actually do.

If the parameters were left blank, a compiler error would occur (in AS3, CS5 at least). You can circumvent this by giving the arguments default values however.
They are needed to give the function (constructor function in this case) the information it needs to execute.
For example, var rect:Rectangle = new Rectangle (0,10,100,20)
The Rectangle function accepts arguments: Rectangle(x:int,y:int,width:int,height:int)
So you’re making a new rectangle at 0,10 that is 100 pixels wide and 20 pixels high.
Or a custom function, like damageEnemy(damage:Number). You need to tell the function how much damage to apply to the enemy.

You’ve used functions with arguments before, surely. attachMovie() needs arguments. In this case, they allow you to set the properties of a new object without all the properties needing to be public.
For the previous example, it is effectively the same as:

var rectangle:Rectangle = new Rectangle();
rectangle.x=0;
rectangle.y=10;
rectangle.width=100;
rectangle.height=20;
 
Flag Post

I’m sorry- but how the heck do you make games without knowing what parameters are? I can’t even fathom making a game without them…

 
Flag Post

The same question came to mind halfway through writing my post, but then I quickly realised that my most recent game had one AS file, no classes, hardly any functions and all of which took no parameters

There is a deceptive drawback to the expressive power that high-level languages offer – unless if you’re taught otherwise, you may consider your solution to be sufficient in terms of making use of the language’s capabilities

The alternative to using parameter-based functions is to use global variables, of course
While you’re probably just joking around, you should realise that there are many AS3 constructs that are unnecessary in most contexts, although obviously nice to have
Like I mentioned earlier, if there was no problem-solving or decision-making aspect to programming, then it would be a lot less interesting than it currently is

 
Flag Post
Originally posted by RTL_Shadow:

I’m sorry- but how the heck do you make games without knowing what parameters are? I can’t even fathom making a game without them…

I was thinking that as well…
 
Flag Post
Originally posted by RTL_Shadow:

I’m sorry- but how the heck do you make games without knowing what parameters are? I can’t even fathom making a game without them…

Haha, it’s very difficult because I have to find lots of creative workarounds. I really should learn AS3 the right way, if only there was some course I could do. Remember that web development class I posted a while back? After we do HTML and CSS I think we’re doing JavaScript. Is AS similar to JavaScript?

I read UG’s post and was halfway through feartehstickman’s before I sort of got it. It was the damageEnemy example that helped. I still don’t see why you can’t do something like this though:

function damageEnemy(){
enemyHP -= damage;
}

UG, you were explaining why your example was useful. I just understood how it works a second ago, but why can’t you just write —


_currentLevel = “level1”;

Using the function doesn’t seem to help too much, like in the damageEnemy example.

Do you guys think it would make sense if I went through the AS3 documentation from start to finish, doing a half hour a day? I know from experience that doing a little everyday really helps when I’m trying to learn something, rather than doing a lot one time and then not doing it again for a while. If I keep at it, maybe I could learn the language.

 
Flag Post
Originally posted by GameBuilder15:

I read UG’s post and was halfway through feartehstickman’s before I sort of got it. It was the damageEnemy example that helped. I still don’t see why you can’t do something like this though:

function damageEnemy(){
enemyHP -= damage;
}

UG, you were explaining why your example was useful. I just understood how it works a second ago, but why can’t you just write —


_currentLevel = “level1”;

Using the function doesn’t seem to help too much, like in the damageEnemy example.

If everything damages for the exact same amount, you wouldn’t need to pass a parametre (necessarily — though for ease of expandability it would probably still be a good idea). What happens when you have 100 different unique damage sources possible (low-damage pellet shots to high damage missiles)? Damage would be passed from the damaging object itself (damageEnemy(missile.getDamage());) to keep everything self-contained and easy to update.

Every level is called “level1”? That seems a bit bizarre to me. Or are you going to write a separate function for each level name init? Imagine it as, say, reading a level from a file, or the user selecting which level they want; the passed-in parametre potentially changes value, which is why you want a parametre, rather than just using a constant.

 
Flag Post

I’m probably missing something crucial here because I still think it doesn’t make much sense.

Why can’t you just do something like this (in the player class):


if(weapon == 1){
damage = 5;
} else if (weapon == 2){
damage = 15;
} else if (weapon == 3){
damage = 100;
}

function damageEnemy(){
enemyHP -= damage;
}

Wait. You’re saying the damage is storied in the projectile itself, right? That’s a good way to do it so if the player switches weapons and a projectile is still in mid-flight won’t do the damage of the weapon the player switched to. But still you could just give the projectile class a damage variable that equals the player’s damage variable at the moment it is created.

I meant, he could manually write


//when you press level 1 button
_currentLevel = “level1”;

//when you press level 2 button
_currentLevel = “level2”

//when you press level 3 button
_currentLevel = “level3”
}

I must be missing something. By the way, what’s a constant?

 
Flag Post

So you’d rather make 100 if/else cases than pass in one parametre? OK, you do that. Your code and .swf will be larger than necessary, you’ll have to manually add another case if you add a weapon, and if the weapon happens to be the last in the list, you go through every single test just to get to it… and for what? Because you don’t want to pass a parametre for some reason. What happens when you consider that these weapons can damage several different enemies? You make this monstrous if/else ladder for each one? Sounds fun.

A var is a value that changes (short for “variable”). A constant is an unchanging value. Rather than hard-coding the number 1 (say) in twenty places and then having to find and change all of them if you decide to adjust it, you declare a constant. Then if you need to adjust the value, you only need to change it in one location. It also helps because then when you come back and look at your code after a while, you can see the name of the constant rather than a mysterious numeral (these are known as “magic numbers”).

Also, in your _currentLevel example, how is the button telling which String to set it to?