Recent posts by zero579911 on Kongregate

Flag Post

Topic: Game Programming / Keyboard problem :O [AS3]

I’m going to quote Draco18s and skyboy for this one (taken from a recent thread http://www.kongregate.com/forums/4-game-programming/topics/256360-as3-keyboard-event):

Originally posted by skyboy:

Originally posted by Draco18s:

KeyDown events fire once:

When the key is pressed.

Hence, keyDown.

If you want your actions to run every frame while the key is held down, then use a variable to determine the state of the key (add an event listener for key up also) and then check on EnterFrame if that variable is true or not.

actually, keyDown fires multiple times (user configurable), keyPress fires once

That said, you could either change the KeyboardEvent.KEY_DOWN to KEY_UP (although it’ll make it so that you have to release the key instead of pressing it) or, make a boolean variable and do something like this:

var downKey:Boolean;
stage.addEventListener(KeyboardEvent.KEY_DOWN, toTrace);

function toTrace(e:KeyboardEvent):void {
    if (e.keyCode == Keyboard.DOWN && !downKey) {
        downKey = true;
        trace("Lol!");
    }
}

This will also fix your problem, (and do what you actually want it to do) but you will have to have another KeyboardEvent listener (KEY_UP). So this KEY_UP listener should look like this (or similar):

stage.addEventListener(KeyboardEvent.KEY_UP, keyUp);
function keyUp(e:KeyboardEvent):void {
    if (e.keyCode == Keyboard.DOWN && downKey) {
        downKey = false;
    }
}

EDIT: Also, if you’re coding in the Timeline, I suggest you change to using classes (which is more organized and far better). You don’t have to though.

 
Flag Post

Topic: Game Programming / Fla runs Faster than Swf?

Oh, I’ll try with a regular player and see if that does anything.
EDIT: Didn’t make a difference. It’s still 40 fps without the web browser running.

 
Flag Post

Topic: Game Programming / Fla runs Faster than Swf?

I also get some weird fps drops (40 fps) when I run my game in a standalone debug player. If I upload it to say, megaswf, I get full 60 fps. Also, if I run it locally in my web browser, it jumps around 40-55 fps (and for some odd reason, it stays at 60 on some rare occasions). What’s even stranger is that if I open up my web browser (doing nothing on it) and play my SWF file locally, I get 55-60 fps (not really stable 60 fps though).

I’m compiling the swf file using HaXe, but HaXe is slightly faster than AS3. I even converted it to AS3 and I still get these weird fps drops. Also, I am blitting my objects so there shouldn’t be any performance drops… Really strange.

If anyone knows a solution, please post.

 
Flag Post

Topic: Game Programming / [HaXe AS3][SOLVED] How to Iterate Backwards Through an Array?

Originally posted by mikebolt:

Completely guessing here:
for (i in a.length … 0) { }

or a while loop

That doesn’t work.
EDIT: Thanks to VBCPP (in GDR) for showing me the link to the answer: http://haxe.org/doc/snip/countdownintiter You were close though, mikebolt.

 
Flag Post

Topic: Game Programming / [HaXe AS3][SOLVED] How to Iterate Backwards Through an Array?

Hi, this seems like a simple question, but I can’t seem to find the answer on google. In AS3 you could do for (var i:int = a.length - 1; i >= 0; --i) { } but in HaXe, you can only do for (i in 0 ... a.length) { } and this only iterates forward.

Thanks.

 
Flag Post

Topic: Game Programming / (AS3) KeyBoard Event

You need to have a boolean variable. After that, in your Key Down event, you need to change the boolean variable to true. Finally, in the Enter Frame event, check if the variable is true and call your functions, whatever you need to do inside of that.

Example:

var wKey:boolean;

function keyDown(e:KeyboardEvent) {
    if (e.keyCode == 87) {
        wKey = true;
    }
}

function keyUp(e:KeyboardEvent) {
    if (e.keyCode == 87) {
        wKey = false;
    }
}

function enterFrame(e:Event) {
    if (wkey) {
        player1.gotoAndPlay(2);
        gstamina -= 5;
    }
}

There is probably errors in that code because I was typing in the reply box instead of typing it in an IDE, but you get the gist of it.

 
Flag Post

Topic: Game Programming / as3 animation loops

Just check if the hero’s right-walking animation is done, then if it’s true, go to the start of the animation and play it again.

 
Flag Post

Topic: Game Programming / moving Large bitmaps

The fastest way would be to just have one bitmap (the size of your stage, no less, no more) and just copyPixels when you need to display a certain part of an image. Having more than one bitmap would obviously slow down the performance even more; even if you aren’t displaying it. Also, doing anything would slow things down by a few nanoseconds or milliseconds. So yes, changing x and y coordinates will slow things down even when the bitmap is not displayed (it won’t be noticeable or take a lot of time, but it still will take time to process).

 
Flag Post

Topic: Game Programming / Change Movieclip's angle?

Originally posted by Shadow_Craver:

I want my character to change angle when he walks up hills. Can I do something like :

onClipEvent(enterFrame) {
	if(_root.char.hitTest(this)) {
		char's angle = 45
	}
}

Is there any code to change a movieclip’s angle when it touches something?

Do you mean like rotation? You could do char.rotation = 45; and it will change it’s rotation.

 
Flag Post

Topic: Game Programming / How do I get object to become visible on the stage.-SOLVED

Originally posted by FlashGrenade:
Originally posted by FlashGrenade:

Okay just so you know Im running the debug in FlashCS4 and im calling
gameengine from the document class in my demo.fla

thus there is no code where gameengine is instantiated.

So you’re saying that the GameEngine.as class is the DocumentClass? If so, then everything should run fine starting from the constructor. I might have missed something in your class but as of now, I don’t see anything wrong.

 
Flag Post

Topic: Game Programming / How do I get object to become visible on the stage.-SOLVED

Originally posted by FlashGrenade:
Originally posted by zero579911:

Yeah, so what’s the problem? That’s exactly what I said (except for making the variable global to the class (private)).

so i change the _game var from private to public and thats it?
there has to be more to it than that.

Sorry if I haven’t replied in a while.

It doesn’t really matter if it’s private or public, the name (_game) doesn’t even matter either. All that matters is that you are calling the GameEngine code somewhere. OR, you could probably make the GameEngine class as the DocumentClass. (With FlashDevelop you can right click the class and click Set as DocumentClass… With Adobe Flash (never really used it) you can go to properties and it will show DocumentClass. Type in the class name that you want to be the DocumentClass in the box.) However, reading your comments for the class (first two (also I didn’t really read all of the code, I’m sorry :P)), it is implying that you’re instantiating the class somewhere.

So again, did you instantiate the code somewhere (var game:GameEngine, etc just like MrRubix showed you)? Or, is the GameEngine your DocumentClass? And when Senekis said that it couldn’t be null, he meant that you already did the var game:GameEngine = new GameEngine() code.

Also, you misunderstood what I said. When I said that “That is exactly what I said (except for making the variable global to the class (private)” I meant that I made a local variable. var game:GameEngine, etc. What he did was made it global to THAT class only, not allowing any other class to use that variable. Which is the private access modifier. Public would be if you have an instance of the class, you would have access to that variable.

if it sounds weird or have grammar mistakes, I’m sorry but I’m a little sleepy.
Just ask me if you have any questions.

EDIT: Also qwerber, he’s having trouble displaying his objects stored in vectors to show up on the stage. He claims that he has successfully gotten the data parsed and that objects are going to the stage, but, all he is seeing is a white screen.

 
Flag Post

Topic: Game Programming / How do I get object to become visible on the stage.-SOLVED

Yeah, so what’s the problem? That’s exactly what I said (except for making the variable global to the class (private)).

 
Flag Post

Topic: Game Programming / My First Game (Preloader)

Originally posted by Weisman4:

Thanks, yeah sry i noticed the post thing after i posted that, well that helps a little and i know where it is now. However i still have never developed anything in this compiler before how d you even compile your project. Or test it or do anything like that i cant seem to find it

You can press F5 to compile the project. Also, do you have the FlexSDK installed? If not you won’t be able to compile your game with FlashDevelop. If you installed FlashDevelop with the default settings, it will automatically configure everything for you so there should be no problem.

 
Flag Post

Topic: Game Programming / How do I get object to become visible on the stage.-SOLVED

Originally posted by FlashGrenade:

I’m tracing out tstage- line 262. the trace says the objects are there.
and they’re not null.

Originally posted by simplegoogly:

I hope you havent forgotten to addChild this GameEngine class when you create its instance.

What exactly does that mean? Can you provide some kind of example or something?

If I’m not wrong, it means that did you do something like this somewhere in your Game:

var gameEngine:GameEngine = new GameEngine();
addChild(gameEngine);
 
Flag Post

Topic: Game Programming / My First Game (Preloader)

If you downloaded FlashDevelop, you can open it up, and click Project > New Project… > Under ActionScript 3 there is an option for “AS3 Project with Preloader” (select it) > Fill in the details for Name, Location, Package, etc… > Then project is created with Preloader.as and Main.as. After that, you can just show something (like a simple text box) and update it.

Example (this can be greatly improved):

package {
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.text.TextField;
	import flash.utils.getDefinitionByName;
	
	public class Preloader extends MovieClip {
		private var
		loadedPercent:TextField = new TextField(),
		loadedPercentText:TextField = new TextField();
		
		public function Preloader() {
			if (stage) {
				stage.scaleMode = StageScaleMode.NO_SCALE;
				stage.align = StageAlign.TOP_LEFT;
			}
			
			addEventListener(Event.ENTER_FRAME, checkFrame);
			loaderInfo.addEventListener(ProgressEvent.PROGRESS, progress);
			loaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioError);
			
			// Show Loader
			loadedPercentText.selectable = false;
			loadedPercentText.text = " % loaded";
			loadedPercentText.autoSize = "center";
			loadedPercentText.x = (stage.stageWidth >> 1) + 20;
			loadedPercentText.y = (stage.stageHeight >> 1);
			addChild(loadedPercentText);
			
			loadedPercent.selectable = false;
			loadedPercent.autoSize = "center";
			loadedPercent.x = (stage.stageWidth >> 1);
			loadedPercent.y = (stage.stageHeight >> 1);
			addChild(loadedPercent);
		}
		
		private function ioError(e:IOErrorEvent):void {
			trace(e.text);
		}
		
		private function progress(e:ProgressEvent):void {
			// Update Loader
			loadedPercent.text = String(int(100 * e.bytesLoaded / e.bytesTotal));
		}
		
		private function checkFrame(e:Event):void {
			if (currentFrame == totalFrames) {
				stop();
				loadingFinished();
			}
		}
		
		private function loadingFinished():void {
			removeEventListener(Event.ENTER_FRAME, checkFrame);
			loaderInfo.removeEventListener(ProgressEvent.PROGRESS, progress);
			loaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, ioError);
			
			// Remove Loader
			removeChild(loadedPercent);
			removeChild(loadedPercentText);
			loadedPercent = null;
			loadedPercentText = null;
			
			startup();
		}
		
		private function startup():void {
			var mainClass:Class = getDefinitionByName("Main") as Class;
			addChild(new mainClass() as DisplayObject);
		}
	}
}

EDIT: Just saw that you said that you are using CS5.5. You can easily put your code into FlashDevelop by copying the classes to your project and then you could make the class that manages everything about your game to be the DocumentClass. After that, if you have MovieClips in your Library, you could export the Library as a SWC file and add it to your FlashDevelop project (don’t forget to right click the swc file and click Add to Library). Once that’s done, it should work, if not the errors should be easily fixed.

 
Flag Post

Topic: Game Programming / PGGC #1: Voting

1 to UnknownGuardian
1 to SkyBoy
1 to BrainStormer

 
Flag Post

Topic: Game Programming / [Performance] Native sort vs. AS3 sort

  64 bit Windows 7 x86 11.1.102.63 Release PlugIn
fastSort     	Vector<Number>	200,000	Numeric	79
Vector::sort 	Vector<Number>	200,000	vecSort	438
fastSort     	Vector<int>   	200,000	Numeric	64
Vector::sort 	Vector<int>   	200,000	vecSort	375
Vector::sort 	Vector<int>   	200,000	intSort	264
fastSort     	Array         	200,000	Numeric	238
Array::sort  	Array         	200,000	vecSort	318
Array::sort  	Array         	200,000	intSort	457
Array::sort  	Array         	200,000	Numeric	128
fastSort     	Array         	200,000	Numeric	184
Array::sortOn	Array         	200,000	Numeric	172
Array::sort  	Array         	200,000	valSort	396
Array::sort  	Array         	200,000	falSort	390
 
Flag Post

Topic: Game Programming / [AS3] [Solved] Any possible ways to add a MouseEvent listener to a Bitmap?

Originally posted by Draco18s:

Add your mouse events to the stage.

Use boundary rectangle math to determine if the mouse is inside the Bitmap’s rectangle.

Oh, never thought of that, thanks Draco18s!

 
Flag Post

Topic: Game Programming / [AS3] [Solved] Any possible ways to add a MouseEvent listener to a Bitmap?

Hi, I looked around the internet and it seems like adding a MouseEvent listener to a Bitmap is impossible.

The only way I can do it is by adding the Bitmap to a Sprite and have a MouseEvent listener on that Sprite, but I don’t want that, I want to have some possible way to check if the user is clicking on the Bitmap. I’m trying to make a fully blitted game; so adding Sprites and other useless things are discouraged.

If there is no possible way, then I guess I’d have to stick with adding the Bitmap to a Sprite.

Thanks.

 
Flag Post

Topic: Game Programming / What is causing my For Loop to fail?

My sprite sheet is 2048×2048, I’m not using the whole thing yet, I only took 15 sprites from the sprite sheet. And, thanks! It works.

 
Flag Post

Topic: Game Programming / What is causing my For Loop to fail?

Hello, I have a very strange problem. For some reason, my for loop seems to fail when I push my class to an array; it stops on the 96th iteration.

Main.as

package {
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.utils.getTimer;
	
	[Frame(factoryClass="Preloader")]
	public class Main extends Sprite {
		// DISPLAY
		private var
		canvas:BitmapData,
		mySpriteArray:Vector.<MySprite> = new Vector.<MySprite>(),
		
		// SHOW FPS
		startTime:Number,
		framesNumber:Number = 0;
		
		public function Main():void {
			canvas = new BitmapData(480, 400, false, 0x000000);
			addChild(new Bitmap(canvas));
			
			startTime = getTimer();
			
			for (var i:int = 0; i < 200; ++i) {
				trace(i);
				mySpriteArray[i] = new MySprite();
				mySpriteArray[i].spritePoint.x = (Math.random() * 480);
				mySpriteArray[i].spritePoint.y = (Math.random() * 400);
			}
			
			addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
		}
		
		private function loop(e:Event):void {
			var currentTime:Number = (getTimer() - startTime) / 1000;
			++framesNumber;
			
			if (currentTime > 1) {
				trace(int((framesNumber / currentTime) * 10.0) / 10.0);
				startTime = getTimer();
				framesNumber = 0;  
			}
			
			var mySprite:MySprite;
			
			canvas.lock();
			canvas.fillRect(canvas.rect, 0x000000);
			for (var i:int = 0, mySpriteArrayLength:int = mySpriteArray.length - 1; i < mySpriteArrayLength; ++i) {
				mySprite = mySpriteArray[i];
				mySprite.animate();
				canvas.copyPixels(mySprite.spriteBD, mySprite.spriteBD.rect, mySprite.spritePoint);
			}
			canvas.unlock();
		}
	}
}

MySprite.as

package {
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	public class MySprite {
		[Embed(source = "SpriteSheet.png")] private var SpriteSheet:Class;
		
		public var
		spriteBD:BitmapData = new BitmapData(140, 130, true, 0x000000),
		spriteSheet:Bitmap = new SpriteSheet(),
		spriteAnimationArray:Vector.<BitmapData> = new Vector.<BitmapData>(),
		spritePoint:Point = new Point(0, 0),
		spriteAnimationIndex:int = 0,
		spriteZeroPoint:Point = new Point(0, 0);
		
		public function MySprite() {
			var bitmapData:BitmapData = new BitmapData(129, 121, true, 0x000000);
			bitmapData.copyPixels(spriteSheet.bitmapData, new Rectangle(671, 252, 129, 121), spriteZeroPoint);
			spriteAnimationArray[spriteAnimationArray.length] = bitmapData;
			
			bitmapData = new BitmapData(137, 117, true, 0x000000);
			bitmapData.copyPixels(spriteSheet.bitmapData, new Rectangle(800, 253, 129, 119), spriteZeroPoint);
			spriteAnimationArray[spriteAnimationArray.length] = bitmapData;
			
			bitmapData = new BitmapData(129, 117, true, 0x000000);
			bitmapData.copyPixels(spriteSheet.bitmapData, new Rectangle(129, 495, 129, 117), spriteZeroPoint);
			spriteAnimationArray[spriteAnimationArray.length] = bitmapData;
			
			bitmapData = new BitmapData(129, 112, true, 0x000000);
			bitmapData.copyPixels(spriteSheet.bitmapData, new Rectangle(129, 612, 129, 112), spriteZeroPoint);
			spriteAnimationArray[spriteAnimationArray.length] = bitmapData;
			
			bitmapData = new BitmapData(129, 107, true, 0x000000);
			bitmapData.copyPixels(spriteSheet.bitmapData, new Rectangle(0, 833, 129, 107), spriteZeroPoint);
			spriteAnimationArray[spriteAnimationArray.length] = bitmapData;
			
			bitmapData = new BitmapData(129, 104, true, 0x000000);
			bitmapData.copyPixels(spriteSheet.bitmapData, new Rectangle(258, 605, 129, 104), spriteZeroPoint);
			spriteAnimationArray[spriteAnimationArray.length] = bitmapData;
			
			bitmapData = new BitmapData(129, 102, true, 0x000000);
			bitmapData.copyPixels(spriteSheet.bitmapData, new Rectangle(129, 829, 129, 102), spriteZeroPoint);
			spriteAnimationArray[spriteAnimationArray.length] = bitmapData;
			
			bitmapData = new BitmapData(129, 103, true, 0x000000);
			bitmapData.copyPixels(spriteSheet.bitmapData, new Rectangle(387, 498, 129, 103), spriteZeroPoint);
			spriteAnimationArray[spriteAnimationArray.length] = bitmapData;
			
			bitmapData = new BitmapData(129, 105, true, 0x000000);
			bitmapData.copyPixels(spriteSheet.bitmapData, new Rectangle(129, 724, 129, 105), spriteZeroPoint);
			spriteAnimationArray[spriteAnimationArray.length] = bitmapData;
			
			bitmapData = new BitmapData(129, 109, true, 0x000000);
			bitmapData.copyPixels(spriteSheet.bitmapData, new Rectangle(258, 496, 129, 109), spriteZeroPoint);
			spriteAnimationArray[spriteAnimationArray.length] = bitmapData;
			
			bitmapData = new BitmapData(129, 114, true, 0x000000);
			bitmapData.copyPixels(spriteSheet.bitmapData, new Rectangle(0, 719, 129, 114), spriteZeroPoint);
			spriteAnimationArray[spriteAnimationArray.length] = bitmapData;
			
			bitmapData = new BitmapData(129, 118, true, 0x000000);
			bitmapData.copyPixels(spriteSheet.bitmapData, new Rectangle(0, 483, 129, 118), spriteZeroPoint);
			spriteAnimationArray[spriteAnimationArray.length] = bitmapData;
			
			bitmapData = new BitmapData(129, 118, true, 0x000000);
			bitmapData.copyPixels(spriteSheet.bitmapData, new Rectangle(0, 601, 129, 118), spriteZeroPoint);
			spriteAnimationArray[spriteAnimationArray.length] = bitmapData;
			
			bitmapData = new BitmapData(129, 121, true, 0x000000);
			bitmapData.copyPixels(spriteSheet.bitmapData, new Rectangle(802, 132, 129, 121), spriteZeroPoint);
			spriteAnimationArray[spriteAnimationArray.length] = bitmapData;
		}
		
		public function animate():void {
			if (spriteAnimationIndex > spriteAnimationArray.length - 1) {
				spriteAnimationIndex = 0;
			}
			spriteBD.lock();
			spriteBD.fillRect(spriteBD.rect, 0x000000);
			spriteBD.copyPixels(spriteAnimationArray[spriteAnimationIndex], spriteAnimationArray[spriteAnimationIndex].rect, spriteZeroPoint);
			spriteBD.unlock();
			++spriteAnimationIndex;
		}
	}
}

Hopefully someone knows what the problem is.

 
Flag Post

Topic: Game Programming / [AS3] [SOLVED] Converting Movieclip to Bitmap faster than just Movieclip?

Originally posted by NineFiveThree:
Originally posted by zero579911:

And also, I want to make everything by myself without using Frameworks, etc.

lol

._. Yes, I know that Adobe made the Classes themselves but I meant like Flixel, Startling and other frameworks. And what’s so funny about that.

 
Flag Post

Topic: Game Programming / [AS3] [SOLVED] Converting Movieclip to Bitmap faster than just Movieclip?

I already know how to create one. It’s just my artist who doesn’t know how, but next game I guess he’ll start learning or something. He says blitting is a bit overkill, lol. :P However, this still clearly works, its faster than just using Movieclips. I’d prefer sprite sheets over this but again, my artist thinks it’s a bit overkill. All he has to do is export the Movieclip’s frames to a PNG file and make it into 1 big sprite sheet.

And also, I want to make everything by myself without using Frameworks, etc.

 
Flag Post

Topic: Game Programming / [AS3] [SOLVED] Converting Movieclip to Bitmap faster than just Movieclip?

Nahh, I didn’t do cacheAsBitmap. What I did was make a class, made a variable to reference the MovieClip from the swc file (I’m using FlashDevelop) and then I drew every frame of the MC onto an Array. I then made a function to play through the whole Array so it animates. (I know I could’ve done it a different way; Example: Make the MC to stop on the next frame and then draw it to the canvas, but I wanted to do it this way since I’m making a game like this.) In my Main.as (not MC Class) I made a bitmapData(canvas) for the whole thing to draw on, then I made a for loop to make a bunch of MCs converted to bitmaps through my class, and then I finally copyPixel every MC’s bitmapData to the canvas.

If there’s a faster way then go ahead, tell me. I’ll be glad to change my way of MC → Bitmap Blitting so it’s even faster.

EDIT: Also, this is my first time testing anything at all. All I did was go on the internet, looked up ways on how to get your FPS and started testing.

 
Flag Post

Topic: Game Programming / [AS3] [SOLVED] Converting Movieclip to Bitmap faster than just Movieclip?

Hello, if you guys are wondering about the answer, I’ve gotten it for you by testing it myself.

The MC contains 58 frames in total.

10 MC’s = 30 FPS
50 MC’s = 20 FPS
100 MC’s = 8-10 FPS
250 MC’s = 3 FPS

100 MC’s converted = Stable 30 FPS
250 MC’s converted = Stable 30 FPS
500 MC’s converted = Stable 20 FPS

This was tested on a 5 year old Laptop, results are going to vary with other computers. This shows that even if you are converting a Movieclip to a Bitmap, it is still going to be faster than just plain movieclips. However, memory usage increases higher with conversion instead of just plain MC’s. Also, the initial load of converting and adding it to the canvas is slower than the initial load of just adding MC’s to the stage.

TL;DR: Converting a Movieclip to a Bitmap IS faster than just using a plain Movieclip.