Recent posts by Moonkey on Kongregate

Flag Post

Topic: Game Programming / ColorMatrixFailter! [AS2] Yeah I did a pun on Filter. So what?

It looks like most of that should be outside the for loop. You’re assigning a new filter every time you check a bullet.

Is BulletBox an Array or a MovieClip? I assume it’s a MovieClip, but if it’s an Array you’d need to splice the bullets out when you remove them.

You might be better off using a ColorTransform rather than a ColorMatrixFilter. I’m pretty sure it performs better, and you can add to the color channels instead of just multiplying the crap out of them hehe.

		for(var i in _root.Masterinst.Bulletbox)
			if(this.hitTest( _root.Masterinst.Bulletbox[i] ) )
		this.transform.colorTransform = new ColorTransform(1, 1, 1, 1, whcount, whcount, whcount);

Flag Post

Topic: Game Programming / AS3: Two ways of iterating through arrays. Which is better?

We did some benchmarks on this a while ago.

for (i:String in array) is the slowest by far in as3 (although fastest in as2)
for each and for (var i:int = array.length - 1; i >= 0; i--) Were faster than each other depending on the testing environment. I choose i— now since its arguably the fastest, and is flexible if you’re removing elements during the iteration.
for (var i:int = 0; i < array.length; i++) is not as fast as the two above, because array.length is expensive compared to a local var, as Draco mentioned.

Flag Post

Topic: Game Programming / Method Optimization

That might not be a fair test since every iteration but the first will be removing nothing. It’s still faster for sure, but maybe not by that much.

For removing a single element from the end, pop() seems to have an edge over length.

Flag Post

Topic: Game Programming / Non-Rectangular Simple Button

It’ll always use the bounding box of a bitmap.

You could break it apart and erase the edges, or trace bitmap, but it would be much easier to just delete the bitmap and draw an oval using the Oval Tool.

If you’re not using the Flash IDE you’ll need to use a Shape/Sprite and graphics.drawEllipse.

Flag Post

Topic: Game Programming / [solved] AS3: How to catch MouseEvents on Sprite that is behind another Sprite?

Bubbling happens up the display list, but it sounds like A and B are siblings, so only the front one is going to dispatch the event.

If you need both to have mouse interaction, I don’t think you can do this using seperate listeners.

I would do this using a workaround with getObjectsUnderPoint. Inside the handler, see if A or B contain any of the elements in the array it gives you. The array is ordered depthwise, so you can figure it which is behind the other.

Edit: If there is only 2 objects to consider, just hitTestPoint would be simpler. But if you have a lot of these objects then getObjectsUnderPoint could be better.

Flag Post

Topic: Game Programming / AS3 How to make a color darker

not for speed; for safety, since adding can overflow into a higher colour while oring prevents it;

Sure, but you have the wrong colour either way.

Flag Post

Topic: Game Programming / AS3 How to make a color darker

You can add them :p

Though OR is faster of course. Good call.

Flag Post

Topic: Game Programming / AS3 How to make a color darker

This is usually done with bitwise operators to isolate each channel into a separate uint, then combine them again.

To get just the blue channel, and the last 8 bits of the color

var b:uint = color & 0x0000FF;

For green, use the middle 8 bits, and shift them right by 8 bits so you can treat it like a normal uint ranging from 0 to 0xFF

var g:uint = (color & 0x00FF00) >> 8;

And the same for red, shifting the first 8 bits 16 places to the right

var r:uint = (color & 0xFF0000) >> 16;

Do whatever operations you want to r, g and b (subtract 1, multiply by some fraction etc), then you can shift r and g back to the positions and add the channels back together again

var newColor:uint = (r << 16) + (g << 8) + b;
Flag Post

Topic: Game Programming / Do you see anything wrong in this code?

Yeah, its always better to run all of your enterFrames off one event.

While looking through some beginner tutorials I noticed that they almost always update the objects with an Enter frame event inside the object, not in the main class

It’s weird, but there seem to be a lot of Flash tutorials written by people that have barely learnt the basics themselves

Flag Post

Topic: Game Programming / Bullet Accuracy

To get an even positive/negative spread, the first thing you need is (Math.random() – 0.5)
That’ll give a number between -0.5 and 0.5

Assuming Acc is some value in radians, you probably want to use
Accuracy = (math.random() - 0.5) * 2 * Acc;
to get a value betwen -Acc and Acc.

Then add Accuracy to Direction instead of multiplying

private function move():void
	this.x += Math.cos(Direction + Accuracy) * Power;
	this.y += Math.sin(Direction + Accuracy) * Power;

So you’ll end up with something between Direction – Acc and Direction + Acc

Flag Post

Topic: Game Programming / GotW #1 Voting

Originally posted by CuriousGaming:

But how can you ‘suck’ in a vacuum?


We may have to disqualify him.

Flag Post

Topic: Game Programming / GotW #1 Voting

RivaledSouls. Classy for a GOTW entry.

Flag Post

Topic: Game Programming / How to add a badges?

It depends what other games are waiting for badges. The last game that got badges has a score just over 3.5, so a game with less than that doesn’t have much chance.

Flag Post

Topic: Game Programming / How to add a badges?

You can’t. The Kong staff have to do it.

All you can do is implement the API, and hope the game scores well enough to warrant badges.

Flag Post

Topic: Kongregate / Getting my Revenue

Paypal works, too.

They make the payments once per month, so you’ll get a payment the first time that comes around when you have over $25.

Flag Post

Topic: Game Programming / Rotate to mouse issue (solved)

Keyboard is a built in class, which has those constants. You’ll need to import it to use it though

import flash.ui.Keyboard;
Flag Post

Topic: Game Programming / Unhighlightable Text (AS3)

textField.mouseEnabled = false;

By the way, you can also turn off selectable in the IDE for dynamic text. Theres a little ‘A’ button in the properties inspector which is on by default.

Flag Post

Topic: Game Programming / Dynamically changing vars

I should have mentioned, you probably want to use currentTarget in this situation.

If you click on one of your object’s children, target would be that child, and currentTarget would be the object itself since that’s what you attached the listener to.

Flag Post

Topic: Game Programming / Dynamically changing vars is the object that dispatched the event, or e.currentTarget is the object you added the listener to (Not always the same thing, especially with mouse events)

Flag Post

Topic: Game Programming / Disappearing Grandparent

Sounds like maybe you’re just recycling the same textfield instance, and it is being added to the new Character’s Menu.

Depending on when you do the trace, that new menu may not have been added to the character yet, which would explain why parent.parent is null.

Flag Post

Topic: Game Programming / Flixel background colour problem

Alpha goes first in 32 bit colors.

FF6400 is no colour because it’s the same as 00FF6400

Flag Post

Topic: Game Programming / [AS2] Keeping MovieClip within Bounds

If the game is really simple and you only plan on having 4 walls, you could take the easy way and just hard code them by calling them different things. For example, “hWall1” and “hWall2” only bounce ySpeed, and “vWall1” an “vWall2” only bounce xSpeed.
But if that’s the case you may as well just hard code in x and y values for the boundaries and use basic checks to see if they’ve gone past a boundary and need to be bounced.

For a more general setup with different wall arrangements, the best way that comes to mind would be to record the previous position the player was in and use that to work out which way he should bounce. You could just use x/y coordinates, but using the edges of the bounding rectangles will be more accurate.
To avoid the wall joints double-bouncing, you can explicitly say which direction they go based on whether they were left/right, or above/below each wall.

function move() {
	var playerBounds = player.getBounds(this);
	player._x += xSpeed;
	player._y += ySpeed;

	for (i = 1; i < 5; i++) {
		var currWall = this["wall" + i];
		if (player.hitTest(currWall)) {

			var wallBounds = currWall.getBounds(this);
			if (playerBounds.xMax < wallBounds.xMin) {
				xSpeed = -Math.abs(xSpeed);
			} else if (playerBounds.xMin > wallBounds.xMax) {
				xSpeed = Math.abs(xSpeed);
			if (playerBounds.yMax < wallBounds.yMin) {
				ySpeed = -Math.abs(ySpeed);
			} else if (playerBounds.yMin > wallBounds.yMax) {
				ySpeed = Math.abs(ySpeed);

Flag Post

Topic: Game Programming / What do you think the best way to do this?

I’d rather go with the 3000 line actionscript file :/

I use XML sometimes, but mainly when there are other people on the project that need to be able to fiddle with constants.

In a situation like this I’d rather plug it straight into code, so I can keep things concise and have the compiler do type checking for me (and not need to parse anything)

Flag Post

Topic: Game Programming / [solved] AS3: BitmapData.draw() method not rendering transparency

When you create the BitmapData, there is an optional third parameter which decides whether it has transparency or not. You should also specify a default color using the fourth parameter.

var tileBmpd:BitmapData = new BitmapData(fillerTile.width,fillerTile.height, true, 0x00000000);
Flag Post

Topic: Game Programming / [AS3] How to check highest/lowest value in an array?

Skyboy’s will order the whole array from best to worst choice. If you only need the best choice you can do it faster with a single loop through the Array like UG said.

var best:Enemy = enemies[0];
for (var i:int = enemies.length - 1; i > 0; i--) {
    var enemy:Enemy = enemies[i];
    if ((enemy.level > best.level) || ((enemy.level == best.level) && (enemy.hp < best.hp))) {
        best = enemy;

After going through that loop, best will have the best choice.