Recent posts by Wordblind on Kongregate

Flag Post

Topic: Game Programming / Language for further game development

Oh so C# is Windows only.

No, compiled C# programs run on the Common Language Infrastructure (CLI), which is an open standard with implementations of various quality on most major platforms. However, most C# programs use the .NET Framework, a proprietary set of libraries that are only fully available on Windows. A Java program that calls upon native code is no more portable. There is nothing to stop you from sticking to cross-platform libraries and making a portable program.

why is Java so wide spread?

1. It came around at the right time. In the early 90’s, when the Internet was young, there were very few free, fully featured, compile-once-run-anywhere, web-browser-embeddable languages available.
2. The JDK came with source code for the API! (Very unusual at the time.)
3. Sun threw around a lot of money into getting books on the language written.
4. About half of the entry-level college programming courses since then have been in Java.
5. A lot of programmers knowing a language means that there is a lot of well-written, well-organized code around. (Good for development and learning.)
6. It’s everywhere: Blu-ray, Android, web servers, compilers, toys, robotics, etc.
7. Oracle has too much invested in the platform to let it fail, so the performance and technology will continue to improve.

But I never hear anything about C#.

Then you haven’t been listening anywhere were Microsoft, Windows, XBox, or Unity is discussed.

To me it looks like Java is slow and bad

99% of everything said about Java performance and optimization is either horrendously outdated or just plain wrong. Here is an example that should give you a rough idea where it stands with basic number crunching. Since the quality of JVM and CLI implementations vary so much, it is tough to say anything except that their performance is in the same ballpark.

Complaints about the Java language and frameworks hold more weight.

Flag Post

Topic: Game Programming / End of file occured error

The encoded string will still be 8/6 as large as the deflated binary.

Besides, it’s not a matter of how large or small the actual cost is; it is the fact that the encoding is entirely superfluous. There may appear to be an advantage in that the encoded string contains no reserved characters—but wait—this is sent over a unsecured connection, and pasting unchecked strings into a SQL statement is a Bad Thing&trade. The data may consist solely of reserved characters and injection attacks; the server must still process the string or use a prepared statement.

So, unless you are using something like the Kong API, which requires you send a string, what advantage is there to encoding?

Flag Post

Topic: Game Programming / End of file occured error

You don’t really need the Base64 encoding; it just inflates the data size by one third and eats up processing time. Believe it or not, there are ways to put actual binary data into MySQL Binary Large Object fields. Two common methods are:

1. When preparing the SQL statement, escape the reserved characters of the binary data (such as single quote, double quote, backslash, and null byte) before putting it into the SQL statement. PHP offers the mysqli_real_escape_string function to do this.

2. Find the MySQL API for your language and use prepared statements. The API will take advantage of MySQL’s binary protocols to send the data unprocessed, saving both bandwidth and processing.

Flag Post

Topic: Game Programming / [AS3] Reliability of removing listeners through weak references

I was wondering how reliable it is to remove event listeners through weak referencing.

That depends on what you mean by reliable. The mechanism is sound, but garbage collection is not required to kick in at any specific time. If no new objects are being created, garbage collection may not occur. A later change to the program may create a reference to the listener, blocking collection. So, you can rely on weak listeners for avoiding memory leaks, but always write such a listener as if it could be called at an arbitrarily distant point in the future.

I have certain buttons which might be added shortly after they are removed, so I don’t want to have multiple listeners on the same button.

A perfect example of when not to rely on weak listeners; the old listeners could may disappear immediately, or a number of factors could keep them around indefinitely.

Oh, also, is there a way to trace all running event listeners?

Well, you could override addEventListener and removeEventListener to keep a running list of listeners, but I don’t really suggest it. Flash’s event system is built with encapsulation in mind; different pieces of code should be able to place listeners on an object without worrying about side effects. Some of classes in the standard API use events internally, and you wouldn’t want to break them.

I suggest that, every time you add a listener you will want to remove later, you add a second that will remove both listeners on a "clear button" event.

Flag Post

Topic: Game Programming / Moving, size changing mask with gradient? (AS3)

There are, however, blend modes. It isn’t that hard too do if you read the descriptions and experiment, but if you have too many moving objects using blend modes, keeping the frame rate up requires a little understanding of the rendering process.

Flag Post

Topic: Game Programming / [AC3] Var and text

If AC3 is an imperative language, what languages are not imperative?

The opposite of imperative programming is declarative programming, were you specify the desired result but not the steps to get there. A simple example found in web pages written in HTML and CSS: the size of different elements can be expressed in absolute and relative units, allowing the page to adjust the page for different resolutions. On the Flash platform, the Flex SDK can compile a declarative language called MXML, which allows the properties of declared objects to be automatically updated to reflect the value of expressions.

There are a lot of areas in computing, such as spreadsheet calculations, which primarily use declarative programming but include other concepts.

There are lots of other types of languages that don’t fit into or partially overlap with the imperative and declarative paradigms, but you can research them yourself if you are interested.

Method #2 I couldn’t get it to work. I didn’t know where to put these lines and what delete or replace in my code. I tried different ways, but didn’t work. Also, I dont understand the code.

Basically, the point was to replace the simple variable pres with two functions that give it’s behavior on the left and right side of the assignment operator (=) and trigger the text field’s update. The get and set keywords are used to declare accessor and mutator functions, which allow you to use variable syntax to call them; pres = 5 will pass 5 to the setter function, and someVariable = pres will call the getter to retrieve the value.

The most likely problem is that you were using the code in the timeline, rather than in a class, so the private and public keywords caused problems. I think if you remove those keywords and make sure that you deleted the original definition of pres, it should work.

The line txt1.text = _pres = value; basically translates into temp = value; _pres = value; txt1.text = temp;.

Method #3 I didn’t understand what you meant.
addEventListener(Event.ENTER_FRAME, updateText);
function updateText(e:Event):void {txt1.text = pres;}
Flag Post

Topic: Game Programming / [AC3] Var and text

ActionScript is an imperative programming language, which means that assignment transfers a value once rather than establishing any sort of permanent relationship between variables. You must add code that executes at the time you want the text field to be updated.

Method #1: Have the code that updates the variable change the text field.

function count(m:MouseEvent):void {txt1.text = pres++;}

This is easy to start with, but it can get complicated once you have a lot of different code dealing with pres.

Method #2: Update the variable through a function that automatically updates the text field.

private var _pres:int = 0;
public function set pres(value:int):void {txt1.text = _pres = value;}
public function get pres():int {return _pres;}

This stays simple, but there is overhead proportional to the number of times the value is changed.

Method #3: Update the text field every frame. This creates a constant amount of overhead.

Flag Post

Topic: Game Programming / scope question

Not that it is the variable rather than the value that has a scope and life cycle associated with the function.

Flag Post

Topic: Game Programming / bullet class

From the looks of it, you never remove the ENTER_FRAME listener, so the bullet keeps going until it is garbage collected. I could be wrong, because you would probably notice if, due to parent being null, an error was thrown every frame.

Flag Post

Topic: Game Programming / [AS3] Better code for mass Buttons (solved)

You can use the same listener for all buttons and use to determine which button was pressed. In fact, the CLICK event bubbles, so you need only add the listener to playMenu.

Flag Post

Topic: Game Programming / Circular hitbox Vs Tiles

If you know the nearest point on the tile, it just becomes a point vs. circle collision. Since a tile is always axis-aligned, the closest point is (clamp(circle.x, tile.left, tile.right), clamp(circle.y,, tile.bottom)).

Flag Post

Topic: Game Programming / File Reference

I don’t think there are equivalents of FileReference.load() or ByteArray in AS2, so you would need to bounce the file off of a server to get access. I think the AS2 equivalent of the Loader class is the loadMovie function.

Flag Post

Topic: Game Programming / [AS3] Any way to copy a multidimensional array?[Solved]

just note that the first return should be an if.


And the fact that it is a shallow copy but the OP is asking for a deep copy. Which I believe can be done through bytearray or something but I don’t remember off the top of my head.

No, it is a shallow copy with reference to the objects contained in the multi-dimensional array. Copying the leaf objects is not always the ideal solution. The arrays themselves are duplicated, which solves the OP’s problem of everything under the first level structure being aliased.

Flag Post

Topic: Game Programming / [AS3] Any way to copy a multidimensional array?[Solved]

copy =;

function shallowCopy(item:*, index:int, array:Array):* {
    if (item is Array)
        return (item as Array).map(shallowCopy);
        return item;

Of course, this is version is fairly slow, but it is easy to optimize it if needed.

EDIT: Actually, I only ever use map in AS3 for short posts or tests. It’s slightly quicker for a reader to understand the algorithm, but it take about the same effort to write and debug a method that doesn’t have the overhead of callbacks.

Flag Post

Topic: Game Programming / bitmap declared as static wont display

A Bitmap, like all DisplayObject instances, can only be added to the display list once, since it has instance-specific information on location, parent, bitmap caching, etc. If you try to add it again, it is just moved. You can, however, share a BitmapData (pixel data) between multiple Bitmap instances by passing it to either their constructors or bitmapData properties.

Flag Post

Topic: Game Programming / [AS3] Increasing width [SOLVED]

Danishdragon: Remember that the object is scaled from it’s internal (0,0), not the top-left drawn pixel. When you want to offset a component on the screen, adjust the x and y properties rather than the coordinates you send to the graphics object.

Flag Post

Topic: Game Programming / How can I erase a movie clip? [solved]


Flag Post

Topic: Game Programming / [AS3] Collission detection

Checking against the map array is a standard and sound method of using space partitioning to simplifying collision detect. Unfortunately, this simplicity is the reason that 99% of platform games end up looking like re-skins of the same game. Remember to “think outside the box.”

Your code doesn’t seem to be quite consistent between rightward- and leftward-facing steps. You get one more pixel of give to the left, allowing you to hang over empty air. You shouldn’t need to consider the down right tile if the character doesn’t straddle it. To make it more confusing, you fixed the behaviour for a hole between two tiles. I think something like if (!map[co + MAP_WIDTH] && (!map[co + MAP_WIDTH + 1] || (x % TILE_WIDTH) + width <= TILE_WIDTH)) would be more consistent.

I really suggest using constants over hard coding numbers such as the block and map width, but that’s your choice.

EDIT: There are two main differences between vectors and arrays:
1. You specify the type of elements that can be placed in a Vector, giving benefits for debugging and execution speed.
2. Instead of only keeping track of only the indexes were elements have been stored, as Array does, Vector has regularly offset locations for every index up to length - 1. That means elements can always be accessed in constant time, but memory use is always proportional to length.

Flag Post

Topic: Game Programming / Extending a function?

If you are using the class setup, you need to use the new operator, so that an instance containing the Act method is created.

Flag Post

Topic: Game Programming / Extending a function?

f = someFunction; // assign the function without calling
f = someFunction(); // call the function, and assign the result

It’s the same principle as when you use addEventListener("eventType", someListener) (or in AS2, onEventType = someListener); you are passing someListener without calling it.

Flag Post

Topic: Game Programming / Extending a function?

Originally posted by zackls:

And word that would be fantastic if constructors could return things.

You don’t need the constructor to return anything; you keep the do function from the new object, and do retains the scope of the object, allowing the code to access the instance variables.

Flag Post

Topic: Game Programming / Extending a function?

You could do it with function closures.

var greet:Function = action("Hello");
package {
	public function action(a:String):Function {
		return function():void {trace(a);};

If you have the outer function take a function argument, you have the rudiments of functional programming (without the efficiency or brevity you would get in another language). Typically, you see it in ActionScript when people want to a customizable event listener or callback function without going through the effort of creating a class.

Which leads to a second option: creating a class to hold the data.

var greet:Function = new Action("Hello").do;
package {
	public class Action {
		private var a:String;
		public function Action(a:String) {this.a = a;} 
		public function do():void {trace(a);}
Flag Post

Topic: Game Programming / Duplicated condition in an if...else block should throw an error

That isn’t to say that it can’t be done—dead code identification is pretty popular tool for finding coding errors—but code analysis is usually implemented as a separate tool. I am not aware of any tools for AS3 that do it, but it wouldn’t be too hard to write a rule for FlexPMD (the code analyzer that comes with FlashDevelop) that could spot repeats and filter those with function-based properties.

Flag Post

Topic: Game Programming / Processing Variable As Object

After two readings, I’m not really sure what you are trying to do, so I’m just going to comment on you code.

I’m not exactly sure how AS2 converts classes into strings, but in AS3 it would be "[class _____]". So, assuming testItem is the actual class name, removeFromInv(testItem, 2, true) will result in both itemRef and _root["[class testItem]_ref"] pointing to the same new testItem instance. So, something like delete[testItem + "_ref"]; itemRef = null; would remove the references, freeing the object for garbage collection. If you don’t know the class in order to calculate the property of _root, you might be able to do something like delete[itemRef.construtor + "_ref"].

I’m not quite sure why you would want store the new object in a property calculated from the class, and I have never understood why AS2 programmers want to store everything in dynamic properties of _root, so I can’t really comment any further.

Flag Post

Topic: Game Programming / to splice or not to splice

Let me state that there never has to be an excuse for using a mathematically sound algorithm. If you want to save time by not checking N elements, do it in an orderly and reliable way.

Calling the moved elements “extra” is kind of like a mechanic saying, “Your brakes are fixed, but putting the wheels back on will be extra.” It is simply puzzling to claim it is better to do an expensive check several times at several locations than to just fully validate the list in one go.

Technically, there should never be a null in the list. Setting indexes to null was the alternative to splicing. It doesn’t really make sense to null then remove, ergo the same loop should handle testing, removeChild, and removal from the list. For obvious reasons, the testing code has to check every element on the same frame, so you can’t just skip elements, especially when an element can be skipped an indefinite number of times.