Recent posts by Phantasmagoria on Kongregate

Flag Post

Topic: Game Programming / Number vs Int/Float

In general, it’s a good idea to keep things simple and only worry about speed once you have enough of your program running that you can profile it to find out where the slow bits are. Optimizing something that only uses a negligible percentage of the program’s execution time is a waste of time.

(You do want to be aware of the time complexity of your algorithms, though.)

 
Flag Post

Topic: Game Programming / Number vs Int/Float

Actionscript does not have a Float type. The Number type encompasses ints, uints, and floats, and thus may be different sizes depending on what the actual value is. (Technically, ints may be different sizes as well depending on whether or not they fit into an atom, since the geniuses at Adobe chose to use 32-bit ints rather than making a type that could reliably be stored in a 32-bit atom.)

An int which is already known to be an int will be faster than an int that’s only known to be a Number, yes, but this is primarily because of the simpler path for operations on that value, not because of its size – there’s no need to check for what numerical representation we’re dealing with before we do anything with it.

 
Flag Post

Topic: Game Programming / Annoying problem =(

That code is horrible, regardless of whether or not it will compile. There’s at least one obvious bug I see, but that’s not important – whatever you’re doing, you should be able to do it without so much repetition. So: what are you trying to do?

I would suggest breaking this up majorly.

No, it should be thrown out entirely and something better should be put in its place.

 
Flag Post

Topic: Game Programming / I. OODesign Fundamental Fundamental concepts and benefits

OOP is a relatively new concept

OOP was started in the 60s, actually: Simula 67 introduced classes, inheritance, polymorphism, and other fundamental OOP concepts. Smalltalk (created in the 70s) was another foundational OOP language (the first to be described with the term “object-oriented”), and it contains an object system which is still more advanced in many ways than that found in Actionscript and other mainstream modern languages.

 
Flag Post

Topic: Game Programming / How do you select multiple movieclips?

You don’t. Make a “container” movieclip, add the grass clips to that rather than to the stage, and move the container.

 
Flag Post

Topic: Game Programming / Lag?!

The lag is almost certainly due to the array filling up with references to movie clips that have been removed from the stage. The function, despite being very badly coded, shouldn’t be causing any lag. (If you want a better function, learn how to separate the parts of your logic so that you don’t have to repeat the same if statement a dozen times.)

 
Flag Post

Topic: Game Programming / Duplicating MCs Question

Wouldn’t (“cheapfood” + i) require you to typecast i into a String?

Actionscript will automatically try to convert things to a different type when needed.

Unfortunately, the automatic type conversion plus using + for both addition and concatenation leads to unexpected behavior sometimes…

 
Flag Post

Topic: Game Programming / Duplicating MCs Question

i think the proper code would be (“cheapfood” + _root.get.next.highest.depth)?
and i think you are adding a number to a var, that should be not possible, or at least “not right”
because cheapfood is not a number right?

You are wrong in quite a few ways. Most glaringly, the + operator is used for string concatentation as well as addition.

 
Flag Post

Topic: Game Programming / Duplicating MCs Question

When you declare a variable within a function, that variable only exists within the body of the function. If you want it to exist throughout the class, you need to declare it within the class definition but outside of any functions.

There may be other problems in your code as well, but that’s the one that stood out to me at a glance.

 
Flag Post

Topic: Game Programming / Problem with linkage

LevelData.ground might involve an additional lookup, if the compiler doesn’t optimize it away, so if you’re accessing it a lot and speed is an issue, using tempMap may give you a slight speedup… but you probably don’t need to bother.

Generally, temporary variables should be avoided as much as is reasonably possible.

 
Flag Post

Topic: Game Programming / Problem with linkage

Try declaring the variable in the proper scope.

Also, if the level data doesn’t change, you’d probably be better off doing something like this:

class LevelData {
  public static var ground = [["part1", [   0, 1300,      0,      0],
                                        [1300, 1950,  (7/13),   700],
                                        ...],
                              ["part2", ...],
                              ...]
}

//then reference it like this, without having to instantiate LevelData:

tempMap = LevelData.ground[curPart][curSection]
 
Flag Post

Topic: Game Programming / [AS2] Indeterminate Number of Function Parameters

In AS2, static typing information is discarded after it’s compiled, so it doesn’t affect anything at runtime anyway. Even in AS3, it probably wouldn’t cause a runtime error due to automatic conversion of types, although the compiler might be annoyed.

I do feel similarly about declaring types; I rarely do so unless it’s needed for speed.

not exactly sure what they’re doing there, but i think they take any number passed as an index and make it a string; so passing it strings directly may actually improve a game’s performance…

If you look at the Tamarin source, it’s the other way around – strings that represent numbers get converted to integers.

 
Flag Post

Topic: Game Programming / getDefinitionByName returns what now?

I’d just have one more static array variable in my document class…

Why would you need that?

If you do keep the system you have in place, you probably should at least abstract the method of getting the tower class that a button is associated with. You could also do it within the TowerButton class, and then setMouseIcon wouldn’t need to do all this other stuff that doesn’t directly have anything to do with the action of setting the mouse icon.

 
Flag Post

Topic: Game Programming / [AS2] Indeterminate Number of Function Parameters

(i needs to be an integer by the way, I don’t know what this fascination is with thinking that arrays use strings as their indexes)

When you use for…in, the loop variable will be set to string values. Even if you’re using an array, it will be set to “0”, “1”, etc, not the integers themselves. So, skyboy’s code is correct.

 
Flag Post

Topic: Game Programming / getDefinitionByName returns what now?

This code, as you might have guessed, is intended to change the look of the mouse pointer to a visual of the tower that the player has selected to build. It’s supposed to mimic the ‘site a building’ system from RTSs. I have to instantiate it, because I need the graphics.

There are a couple of ways you could set up a system to do that. The way which seems best to me involves multiple inheritance, which you can’t do in Flash, so blah. But, really, that’s kind of a tangent. It sounds like it is correct for you to instantiate something, whether it be the tower or a placeholder graphic that just looks like the tower.

You can probably do it better without the getQualifiedClassName/getDefinitionByName hackery, though. Why not have the button keep track of which tower class it’s associated with, and then set mouseIcon to a new instance of that class directly?

 
Flag Post

Topic: Game Programming / getDefinitionByName returns what now?

No, I don’t understand the difference; I’m informally trained. What’s the difference?

A class is essentially a “blueprint” for a type of object – for instance, you might have a Tower class that specifies “tower objects will have a variable named this which defaults to this value, a function named that which does that, etc”; this is a different thing than the actual towers which will be created when you run your program.

In Actionscript, the “blueprint” is itself an object of type Class. This is why getDefinitionByName returns an object. In your case, you seem to want a particular object of that type rather than the blueprint – if it doesn’t matter which one it is, you can instantiate a new object based on the class definiton (e.g. new (getDefinitionByName(something))), but you should probably redesign your program so that you don’t need to instantiate it. If you want a particular object other than a class definition, you’ll need to use some method other than getDefinitionByName.

getDefinitionByName is for getting classes – even if it could return other objects, it wouldn’t know which one you wanted; there could be many objects of type Tower, or none, and even if there’s just one Flash doesn’t keep track of such things.

 
Flag Post

Topic: Game Programming / getDefinitionByName returns what now?

getDefinitionByName returns the class object. This will always be an object of type Class, not a MovieClip. What are you expecting it to do? Do you understand the difference between classes and objects?

I’m not sure where the $ is coming from, though, unless that’s just how Actionscript prints class objects.

 
Flag Post

Topic: Game Programming / [AS2] Indeterminate Number of Function Parameters

The solution probably shouldn’t involve string manipulation. And, while using the function parameter array would work, I imagine an object would be a better choice, since they’re designed for key-value pairs. If you send an object like {B:1, C:2}, you can convert that to XML pretty easily.

 
Flag Post

Topic: Game Programming / [AS2] Indeterminate Number of Function Parameters

(it negates the whole point of the function: creating an XML object to send to a server, if I was going to do this outside of the API I’m building then there’s little point in building the API).

What kind of input do you have, and why do you need a function that accepts a variable number of inputs to convert it to XML?

XML is common; chances are there’s an easier way than whatever you’re trying to do.

 
Flag Post

Topic: Game Programming / Broad Phase Collision Detection.

How does not creating a reference make code less efficient?

Your code creates a reference. There’s no way you could do anything with an object if you don’t have a reference to it.

You just don’t keep that reference around, so you end up needing to recreate it later on. It’s like if you needed a screwdriver for putting together some furniture, and you can either buy a screwdriver and use it every time you need it (the original way), or throw it out after every screw and then go to the store and buy a new screwdriver when you need it again (your way).

 
Flag Post

Topic: Game Programming / Question about variable assignment in classes.

Are you expecting the onLoad to run as soon as the shape is created? ‘Cause it won’t. That’s what constructors are for. The onLoad will run as soon as the event triggers, but the event won’t trigger until after the code that creates the shape finishes running (events never interrupt your code), and that’s after you update the display variable.

Additionally, instead of that long if/else statement, you could do something like this:

static var shapedatas = [[1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 2, 2],
                         [2, 3, 3, 4, 2, 2, 3, 3, 2, 3, 3, 4, 2, 2, 3, 3],
                         ...];

//then, to fetch one of them:

shapedatas[shapeType]

It may be even better to use a different class for every shape (all of which would inherit from your base shape class, which would contain all the non-shape-specific code), but that depends on how you have things set up and what you’re comfortable doing.

 
Flag Post

Topic: Game Programming / Broad Phase Collision Detection.

thing is, 2D’s looking for executional optimization, whereas Unknown is going for memory/space optimization.

No, that’s not the thing. Unknown’s code accomplished neither speed nor space optimization.

Protip: the number of lines in the source code is not an indication of how much memory the running code will consume.

 
Flag Post

Topic: Game Programming / Broad Phase Collision Detection.

explain why it would make it slower

Because you’re looking up the same object multiple times. this[boxSort[i]] is not “what tar is”; it’s a series of instructions (lookup element i in boxSort, lookup the result of that in this) that the computer will have to execute every time. Doing that once and storing the resulting value in a local register will be much faster than doing the lookup multiple times. And even if you didn’t store the value in a local register, it’d still have to store it locally somewhere (namely, on the AVM’s stack), because the computer wouldn’t be able to do anything with it if it didn’t.

Generally, computers do not manipulate objects in memory directly. Instead, they copy the objects into registers, execute instructions on the values in the registers, and then write the results back into memory. If there is a value which is used multiple times, it can be kept in a register (provided sufficient space is available), thus saving significant amounts of time.

The AVM, while not a hardware computer, works similarly – functions have local registers to store values in, and accessing these registers is much faster than looking up values that are hidden away behind multiple layers of indirection.

If you’re going to apply “logic” to something like the amount of memory a variable takes up, it would behoove you to learn things like what a variable actually is. There’s no shame in guessing when you don’t know something, but there is shame in pretending that you’re doing any more than guessing when you’re not.

(Admittedly, “what a variable actually is” is a moderately complex topic, and is one of those things that tends to be glossed over and badly explained, so you’re not the only person around with such a bad understanding of it…)

Bahaha, I had no idea that I could be this inane. I must have melted my fuckin’ brains working on this game. Yeah, the second loop is totally unnecessary, as is storing arrays. I’m almost proud of myself for failing this hard.

Hehe. :)

Hopefully fixing that will get the code running sufficiently quickly!

 
Flag Post

Topic: Game Programming / Broad Phase Collision Detection.

Logically, you would not be wasting memory creating a variable and accessing it. I don’t know if this would help much, but give it a try!

Are you really that stupid, or do you just pretend to be stupid online? Your suggestion would make the code slower.

Also: that’s not how computers work. Creating that particular variable probably won’t use any more memory than would otherwise be used, and may well save memory over your suggestion. Either way, a reference only takes one atom, which is a completely unimportant amount of memory.

If you’re going to try to use logic, you must have a basic familiarity with the domain, else you’re really just guessing and most likely consistently wrong.

 
Flag Post

Topic: Game Programming / Broad Phase Collision Detection.

I can’t think of any sort of trivial optimization

I can. You repeatedly do calculations within loops that could be done outside of them. The length lookups are probably the most time-consuming, but others could be raised out of the loop they’re in as well.

What does boxSort contain, and why do you need to lookup whatever it contains with this[]? You might be able to get rid of some (probably slow) dynamic property lookups there.

Wouldn’t it be more efficient to check if two particles are overlapping vertically at the same time as when you check if they’re overlapping horizontally? I don’t see any reason why you need to sort by Y and iterate through the whole thing a second time. You also wouldn’t need those “hits” arrays, which probably take quite a bit of time to create, manipulate, and search through.