Recent posts by JWBSoftware on Kongregate

Flag Post

Topic: Game Programming / Enemy Health AS@

(_root.live = 0)

should probably be

(_root.live == 0)

 
Flag Post

Topic: Game Programming / Help!: int. can't be null

Try changing
if(xtimes.length = 0) {
to
if(xtimes.length == 0) {

 
Flag Post

Topic: Game Programming / How to store bunch of objects?

A new Vector of 2000 objects does not use (2000 * object size) memory, just (2000 * reference size), so a few kb. Really nothing to worry about. If your IDs went up to a million maybe, but 2000 is not a problem.

As you populate the Vector with objects the memory is allocated for each object as it’s created, the same as when you have an Array of them, so the main memory cost is your objects. Though even with 2000 chances are they use less memory than a single graphics object or sound.

Vectors only do things differently for primitive types – int, Number, uint, Boolean – when they allocate memory all at once, which is far more efficient than a separate allocation for every number which an Array has to do.

 
Flag Post

Topic: Game Programming / [AS3] Pass by value

Make the function yourself: there is no clone() method as there is for BitmapData, and I think there’s a good reason for it: not only would it be little needed but you can probably do far better copying the object yourself, knowing what data needs to be copied, what needs to be modified when copied, which if any referenced properties also need cloning, what can be ignored or will be initialised by the default constructor and doesn’t need reinitialising, and so on.

 
Flag Post

Topic: Game Programming / shader filters - yes, but they work incorrectly sometimes

There are at least four ways you can use to embed shaders. The two I’ve used, both starting with .pbj files created in the Pixel Bender Toolkit, are

  • Flex’s embed syntax. E.g. Here’s code from my current app:
[Embed("../Shaders/wave.pbj", mimeType="application/octet-stream")]
private var filterWaveClass:Class;
gWaveShader = new Shader(new filterWaveClass() as ByteArray);
gWaveShader.precisionHint = ShaderPrecision.FAST;
gWaveData = gWaveShader.data;
gWaveFilter = new ShaderFilter(gWaveShader);
// and then when needed:
gWaveData.fAmplitude.value = [fFilterAmp];
gBoard.filters = [gWaveFilter];
  • Embed them as text, which I used on Wonderfl where you can’t embed assets. Here’s an example which you can see working

http://wonderfl.net/c/oHVd

The shader code is in a comment at the end. The shader is embedded as a Base64, which is done using the (unix/OS X) command

openssl base64 -in Life.pbj -out Life.b64

(The other two methods are loading the shader from a URL and including the assembly in a string which gets compiled by Flash. I’ve not tried either of those)

 
Flag Post

Topic: Game Programming / shader filters - yes, but they work incorrectly sometimes

I’ve had no problems running shaders without it hitting the frame rate. There is a performance cost as for everything but it’s no worse than many BitmapData functions, and is often much better. If you’re seeing a 0.5s redraw it suggests you’re doing something wrong, or at least that there’s significant room for optimisation.

 
Flag Post

Topic: Game Programming / shader filters - yes, but they work incorrectly sometimes

I’ve been using Pixel Bender in my latest project, and posting samples on my blog and on Wonderfl

http://johnblackburne.blogspot.com/search/label/Shaders

Once you get used to it it’s a powerful tool, with which you can quickly and easily generate effects that are slow and very difficult to do in ActionScript.

 
Flag Post

Topic: Game Programming / Finding the centre of gravity of a 2D object

Originally posted by jasonjie88:

What about calculating a centre of gravity by weighting the vertices based on their y-values? For example, if one point is higher than another, it weighs more.

No. One way to think about it is “what if I added a vertex?” E.g. add a vertex between two others. Move it slightly if you want so it’s no longer on a straight line between them, though that’s not needed. Then ask “what difference does that make?”.

If you’re just summing and averaging vertices then adding one more will tip your sum towards that vertex. And as one can be added so can ten or 100 on one side, without changing the shape at all.

If it’s triangles then adding a vertex creates a new triangle, or splits an existing one in two, but they are smaller and add up in size to the large one. So they contribute the same to the COG calculation. In fact adding vertices to create triangles can be part of triangulation to do the calculation.

 
Flag Post

Topic: Game Programming / Finding the centre of gravity of a 2D object

Average of the vertex positions won’t work. The image above is an example; it has far more vertices on the left side, so an average would be to the left of the actual centre.

I would do it by triangulating the object, if the shape is polygonal. The centre of each of those is 1/3 from each face, and the total COG can be found by doing a weighted average, weighted by each triangle area (as if the shape is uniform the mass and area are proportional).

 
Flag Post

Topic: Game Programming / Error in AS

You don’t have to invoke a green and pointy-eared warrior to justify such an ordering. E.g, considering a lottery win one might say:

“If the winner is me I will give up work”
or
“If I am the winner I will give up work”

Of course “If the winner is I” is more grammatically correct, though people look at you funny if you speak like that. And most people would just say “If I win [the lottery] I will give up work”.

 
Flag Post

Topic: Game Programming / Error in AS

What they wrote about ‘=’ and ‘==’. If you’re using the Flex compiler it will warn you of such things as they’re easy to miss. Or get in the habit of reversing such clauses, i.e. write them like this:

if (1 == movedirection)

Then if you leave out one of the equals signs you have something that’s impossible so which won’t compile.

 
Flag Post

Topic: Game Programming / Trigonometry vs. dictionary efficiency

It’s common to use lookup tables, but they look very different to that. You prepopulate a simple table, which in Flash’s case would be a Vector.<number>, with as many values as you need. Something like every degree might be fine, you probably don’t need any more accuracy than every 1/10 of a degree (so 3600 entries for the whole circle, using only a few kB of memory).

The best thing though is avoid trig if you can; most things can be done without them by working with directions and vectors.

 
Flag Post

Topic: Game Programming / Detecting Object within a Radius

Originally posted by Aceeri:

var houseradius:Boolean = ((house.x – player.x) * (house.x – player.x) + (house.y – player.y) * (house.y – player.y) < dist * dist);

So I get how this works, but how can you use it as a function of sorts?

I wouldn’t use a function: it’s overkill for a single line like this (functions are slow in Flash). If you are testing multiple points do it in a loop, finishing the loop once it’s in range. Only a couple of lines.

 
Flag Post

Topic: Game Programming / Detecting Object within a Radius

Yes, though you don’t need the square root. E.g

var bLightOn:Boolean = ((house.x – char.x) * (house.x – char.x) + (house.y – char.y) * (house.y – char.y) < dist * dist);

If your house isn’t a single points test a number of points and turn on the lights if it’s close to any of them

 
Flag Post

Topic: Game Programming / 60FPS

Originally posted by Ace_Blue:

Can you alter the FPS at runtime?

You can use

stage.frameRate = 30/60/whatever

at any time

 
Flag Post

Topic: Game Programming / 60FPS

Yes. Don’t do Timers. Apart from the accuracy they are event based and so slow. The fewer events your code has the better. Anyway you don’t need them.

In theory a Timer lets you run code ‘between’ frames. But there’s no point doing this as the player won’t see updates any earlier. So do everything you need in your main event handler, by calling your update code more than once or use a variable time step. This also lets you do optimisations: if you do multiple updates only the last has to move any graphics objects.

Then just call getTimer occasionally. Not too often as it’s not that accurate and anyway you’re not worried about whether any one frame is running slow (which it could do for any number of reasons), but whether the game runs at 30 or 60fps over five or ten seconds. So call getTimer twice, say 300 frames apart.

Then use that to see if the time is running fast enough, by doing time/300 and comparing it to your expected frame rate. If it’s too different then compensate accordingly. Keep sampling every few seconds and keep adjusting the level of compensation.

 
Flag Post

Topic: Game Programming / Flash CS5 changes properties by .05

That’s right. Flash internally uses Twips for screen positions. Twips are 1/20 of a pixel, so anything between e.g. 0 and 1/20 is rounded to one or the other (I’m not sure which). 1/20 of a pixel is too small to see, which is probably the point, but if you use the positions in your own calculations it can introduce errors.

The solution is never use the positions of DisplayObject and similar classes in your own calculations. If your objects use Bitmap instead subclass Bitmap so you can add your own x and y. Or separate your game logic and rendering more cleanly, so the classes describing game logic are independent of the game graphics.

This has the other benefit that it’s much much faster to use your own x and y. The x and y of a DisplayObject and derived classes use accessors which are slow. Better to avoid them except when you actually need to place or move something onscreen.

 
Flag Post

Topic: Game Programming / How can I program this?

You need to know some 3D for that. You may be able to do, or need to do, a lot of the drawing in 2D by e.g. using fixed backgrounds, or using appropriately pre-calculated/pre-rendered assets, but you’re drawing a 3D scene. In particular it’s a perspective projected 3D scene, where things change size and shape based on how they’re viewed.

So have a look at 3D maths, especially perspective projection which is also an artistic technique. You do not need to use 3D rendering: especially in Flash people have made 3D looking games but with 2D rendering. But you need to understand how the 3D view and 2D rendering of it relate.

 
Flag Post

Topic: Game Programming / [Fixed but not understood] Blur filter & cacheAsBitmap causing CPU spikes every 10 seconds.

You’ve not posted any new code, so a couple of comments on your old code.

You don’t need to use a Sprite for the drawing, use a Shape. It’s basically a container for the Graphics class. A Sprite is one but it’s also an interactive object and a container, so is e.g. more things for the GC to check.

This:

bdatab.filters = [iceblur];

Is only needed if the filter’s new or changed. You don’t need to do it every time through if it’s the same each time.

It may just be though that it’s a big bitmap and expensive operation. 2000 × 2000 is very big: the user can only see a fraction of this at a time so the filter can be to a much smaller area. Framespanning can also help: e.g. do a filter with twice the effect every two frames.

As I noted before cacheAsBitmap is only of use in limited situations. Personally I don’t use it: if I want graphics art to be treated as a BitmapData I use draw() to render it into one, usually using filters or shaders to apply effects as I do so. So I have my filtered graphics in a bitmap which Flash can render very quickly.

 
Flag Post

Topic: Game Programming / Speedup code. Draw and copyPixel alternatives.

It looks like your ColorMatrixFilter is just doing a 0.99 fade, i.e. it’s multiplying the alpha channel by 0.99. The following ColorTransform should do the same

ct = new ColorTransform();
ct.alphaMultiplier = 0.99;

ColorMatrixFilter is only needed for transformations between channels, for e.g. recolouring art. But you aren’t doing that, that I can see.

 
Flag Post

Topic: Game Programming / Speedup code. Draw and copyPixel alternatives.

Filters are slow. I’ve found if you are doing them continuously you should find a way to minimise the area affected or to frame-span the effect, i.e. do it only every other frame (maybe alternate them).

Do you really need a ColorMatrixFilter? For that particular filter a ColorTransform, applied using the BitmapData’s colorTransform function, would I think look identical and much quicker. A ColorMatrix is overkill.

Another thing is you’ve a lot of calls to new to create new objects in your update loop. You don’t need any. You can e.g. create tempBmd once and call fillRect to empty it for re-use. your Point(0, 0) can be created once at startup and used wherever it’s needed, as can the BlurFilter. _containerData.rect and tempBmd.rect can also be got once and stored.

Finally dereference bulletArr[jj] for a minor speedup; i.e. do bullet = bulletArr[jj] and then use bullet in place of bulletArr[jj]. bulletArr[jj] is actually a function call and you should not call a function repeatedly to get the same result if you can get away with doing so once.

 
Flag Post

Topic: Game Programming / Game monetisation

If you get a game sponsored some sponsors are able to use other payment methods, such as MoneyBookers or Western Union. But this depends on the sponsor: I suspect it’s mostly larger ones that buy a lot of games.

This doesn’t help with other sources of income though such as Kong, advertising revenue. For that you might need to find someone in a country like the US who can collect such money for you into e.g. their Paypal account. It will need to be someone you trust, maybe someone you have a relationship with such as someone you’re collaborating with on a project.

 
Flag Post

Topic: Game Programming / Game monetisation

Yes, there are no age limits. Make a good game, find someone to buy it/sponsor it, and you’re set. The only limit is you might not be able to be paid: things such as PayPal may require you to be 18 and/or have a credit card. But for that if you get that far you can recruit an adult and get the money paid into your parent’s/brother’s/friend’s account.

 
Flag Post

Topic: Game Programming / Moving TextField stuttering

Render it to a BitmapData and scroll that. It’s worth doing for performance reasons anyway, assuming the text doesn’t change every frame (and what text does? – players need more than 1/30 second to read it). You can also use all the functions of the BitmapData API to make your text far more interesting than what’s possible with the TextField class.

 
Flag Post

Topic: Game Programming / 360 degree movement WITHOUT the math class?

The point about using the normalised direction (so using sqrt) then passing it directly to a Matrix is it’s faster as it avoids two uses of trigonometry. The atan2() used to get the angle. Then the sin() and cos() used to convert the angle to a direction vector for any rendering and transformation, internally in the DisplayObject if you set its rotation.

You also avoid the radians to degrees conversion (that it takes degrees suggests setting rotation is slow). And for no extra cost you can set pass the position of the object at the same time to the matrix,