Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411489 Posts in 69377 Topics- by 58433 Members - Latest Member: Bohdan_Zoshchenko

April 29, 2024, 05:28:00 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)in this installment our gentlemens try to learn the ancient power of FLASH
Pages: [1]
Print
Author Topic: in this installment our gentlemens try to learn the ancient power of FLASH  (Read 4196 times)
oneup
Level 1
*


worth 1000points


View Profile WWW
« on: August 02, 2008, 11:29:54 PM »

it sucks in so many ways, but i made a basic pong game in flash actionscript 3.

soon i shall be able to harness the power of adobe to enslave humanity make kickass games that work on many platforms!!!

is there someone else, who would either like to learn flash or can help a fellow programmer (switching after quite some years years of c/c++, python, ruby))


and now, without further ado:

PONG!

KLICK THIS LINE FOR MOTION

Code:
function keyDownListener(e:KeyboardEvent):void {
trace("e.keyCode=" + e.keyCode); // console print
}

stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDownListener); // keyboard input!!

stage.addEventListener(Event.ENTER_FRAME, gameloop); // calls gameloop()  12 times per second with flash default settings

if (ball.hitTestObject(right_paddle)) // collision detection


and now the real pitch:

* since 0:00am i'm learning flash
* i'd like to use this thread to share some flash insights with you (as most resources on the web suck ass).
* if you like.


why?

see... i've been programming for several years now, when switching to flash there are a just couple of things i need to know to get started. how to get input, how make a gameloop, how do basic collision detection, etc.

yet it took me ages to gather those simple things (the couple lines above took me 3 hours to gather). either i'm stupid, or i don't know. i prefer the later, so i gather some information here for others to use.

usefull yes/no?
« Last Edit: August 02, 2008, 11:39:49 PM by oneup » Logged
Farbs
Man
Level 10
*


/Farbs


View Profile WWW
« Reply #1 on: August 02, 2008, 11:38:13 PM »

Nice one. I'm also going through my regular "maybe I should learn Flash" phase, so I totally feel your pain.

Is it safe to hardcode scan codes for your keys like that? I used to do that for my games in a different environment, and it screwed me when my games were ported to other platforms.
Logged
oneup
Level 1
*


worth 1000points


View Profile WWW
« Reply #2 on: August 02, 2008, 11:54:36 PM »

well... most tutorials i've seen so far did it like that.

i have no idea, but assume that the flash player adds an additional layer of obfuscation abstraction and translates keycodes from differen systems to a unified set for flash applications.

i'd love to use something like Keycodes.UP though. maybe it exists somewhere inside flash. i just have to find where.... (oh and if i do, i'll put it here, of course)
Logged
bateleur
Level 10
*****



View Profile
« Reply #3 on: August 03, 2008, 01:17:50 AM »

is there someone else, who would either like to learn flash or can help a fellow programmer (switching after quite some years years of c/c++, python, ruby))

Happy to help if you have further questions.

A couple of notes about what you have there:

1) Binding things directly to the stage works, but is probably not a good idea for anything more complex than pong. I'd recommend making a Sprite, making it a child of the root using addChild() and then attaching all your event listeners and so on to that. Why is this good? Because when you remove it from the display list it then stops it from picking up further events. The main good thing about moving to Flash from C/C++ is that you no longer have to write vast blocks of boilerplate code (or heavyweight preprocessor macros) to carefully ref/deref, add/remove, create/destroy and so on. Let the garbage collector and the display list do the heavy lifting for you.

2) It looks a bit like you've been writing ActionScript straight into the frame? If so, I'd recommend working with classes instead. So all you have in the actual frame is something like:

var game:MyGameClass = new MyGameClass(this);
game.launch();
stop();


3) For most games it's more useful to be able to tell whether a key is down than to receive an event when it's pressed. I'd recommend writing a helper class which manages keyboard status, but all you really need is an object. So you do something like this:

Code:
import flash.ui.Keyboard;

var keysDown:Object = new Object();

function keyDownHandler(ev:KeyboardEvent) {
 var code:Number = ev.keyCode;
 keysDown[code] = true;
}

function keyUpHandler(ev:KeyboardEvent) {
 var code:Number = ev.keyCode;
 delete keysDown[code];
}

function keyIsDown(code:Number):Boolean {
 return Boolean(keyCode in keysDown);
}

gameScreen.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
gameScreen.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);

// Usage example:
if (keyIsDown(Keyboard.SPACE)) {
 // Fire laser!!!11!
 game.addHugeLaserBolt(gun.x,gun.y);
 game.audio.newSound("pewpewpew.mp3");
 game.character.monocle.popOut();
}

Quote
i'd love to use something like Keycodes.UP though

Take a look at the properties declared in the flash.ui.Keyboard class.

And finally, due to limitations of the TIGS forum software, two rogue BBCode tags will appear after the end of my post. See if you can work out why... Wink

[/code][/code]
« Last Edit: August 03, 2008, 01:23:42 AM by bateleur » Logged

oneup
Level 1
*


worth 1000points


View Profile WWW
« Reply #4 on: August 03, 2008, 11:57:59 AM »

ah, thanks a lot.

so... flash propagates key events  to all objects on the stage?

also; when you say Sprite... are those MovieClips or another class?

and finally: do you know how i can get the width and height of the stage? stage.width and stage.height seem to give me the width and height enclosing all objects in the display list (so... objects that leave the stage's predefined resolution make stage.width and stage.height bigger. o_O)
Logged
X-Tender
Level 1
*



View Profile WWW
« Reply #5 on: August 03, 2008, 12:53:12 PM »

@bateleur: When he codes in AS3 i realy think he use the DocumentClass feature.
So you doesnt have to create all the..
Code:
var game:MyGameClass = new MyGameClass(this);
game.launch();
stop();
.. stuff.

I also use nearly the same Keyboard Input Code to catch User Input Smiley

And to put everyting in a Sprite Object is good to remove all things. but its better to remove the eventListeners one by one before deleting it.

@oneup:
I usually create an static variable for myself where is set width and height of the game. and then access to that variables to get the dimensions.
e.g.
Code:
private static var STAGE_WIDTH:uint = 800;
private static var STAGE_HEIGHT:uint = 800;

mmh I doesn't relay understand your question about the key event propagantes ..

You can give nearly every Object an keyboard Event Listener ..


Nice Thread, I hope I can help you and others a little bit too..
I code also in AS2 + AS3
Logged

isaac
Level 2
**



View Profile WWW
« Reply #6 on: August 06, 2008, 05:50:36 PM »

also; when you say Sprite... are those MovieClips or another class?

Sprites are just a simpler version of MovieClips. Better to use when you don't need to hold animation inside.
Logged

bateleur
Level 10
*****



View Profile
« Reply #7 on: August 08, 2008, 12:47:06 AM »

so... flash propagates key events  to all objects on the stage?

To all objects in the display list, yes.

Quote
do you know how i can get the width and height of the stage?

Cache the values somewhere during your setup stage before you start messing with the content.

When he codes in AS3 i realy think he use the DocumentClass feature.

Sure, that works, but I don't recommend it. You want to be doing everything with classes. Embedding stuff in the SWF is messy and may cause problems later because you lose flexibility over what order your classes are instantiated in.
Logged

X-Tender
Level 1
*



View Profile WWW
« Reply #8 on: August 08, 2008, 02:02:37 AM »

o_O When you use the DocumentClass .. you use classes .. (Spot the string "Class" in "DocumentClass") There's no embedding.

I use it and i didnt have any other code in my fla.
Even your
Code:
var game:MyGameClass = new MyGameClass(this);
game.launch();
stop();
stuff is more then I would prefer..

you just have to place "MyGameClass" as the "DocumentClass".
Logged

bateleur
Level 10
*****



View Profile
« Reply #9 on: August 08, 2008, 05:53:13 AM »

you just have to place "MyGameClass" as the "DocumentClass".

Sure, but if you do that then your SWF will be an instance of your game class. That's not an insurmountable problem, but where I could - much later in my design process - change my launch code to this:

Code:
var menu:MyOuterMenu = new MyOuterMenu(this);
menu.launch();
stop();

...with no consequences, if you do the same thing by simply changing the DocumentClass you'll be changing the relationship between your game and the display list (previously it was the root DisplayObject and now it isn't).

I'm certainly not saying you can't do it that way or that any of the difficulties will leave you completely screwed, it's just not very elegant.
Logged

X-Tender
Level 1
*



View Profile WWW
« Reply #10 on: August 08, 2008, 06:10:53 AM »

I wouldn't say that. It's the proper way in my opinion. You have a main class (the document class). And there you can, like in your example, create the gameClass.
e.g.:

DocumentClass:
Code:
package {
import flash.display.MovieClip;

public class MainClass extends MovieClip{

private var myGame:MyGameClass;

public function MainClass() {
init();
}

private function init():void {
myGame = new MyGameClass(this);
myGame.launch();
}
}

}

sure, you could paste also your type of code in the first frame of the timeline, but with the DocumentClass its much nicer .. And you will find that way of code in every good Tutorial.
Logged

oneup
Level 1
*


worth 1000points


View Profile WWW
« Reply #11 on: August 12, 2008, 10:24:41 PM »

yeah, that's how i do it currently.

now if i could only access .png files in my flash. *sigh
Logged
X-Tender
Level 1
*



View Profile WWW
« Reply #12 on: August 13, 2008, 12:44:44 AM »

did you have it in your flash library? Or did you want to load it dynamicly from outside of the swf? (Or did you use Flex?)
Logged

gnat
Level 1
*



View Profile WWW
« Reply #13 on: September 15, 2008, 01:37:12 AM »

Actionscript 3 is actually very very nice once you've learned the ropes.  Beer! I've been developing a physics based game for a client recently. It has been, for the most part, a pleasure to work with so far. I wouldn't go back to AS2.

I hear the next version of flash will take advantage of the 3D accelerator.

The only craptastic thing about flash right now is its lack of UDP support (true real time multiplayer isn't reasonably possible at the moment); although I hear some sort of UDP support is coming in Flash player 10.
Logged

LAN Party List - The definitive LAN party list. Also Game Jams, etc.
GitHub
Rory
Level 4
****



View Profile
« Reply #14 on: September 15, 2008, 06:19:23 PM »

Have a look at www.emanueleferonato.com. He does good tutorials on AS2 and AS3.
Logged
Farbs
Man
Level 10
*


/Farbs


View Profile WWW
« Reply #15 on: September 15, 2008, 07:00:36 PM »

now if i could only access .png files in my flash. *sigh
I'm pretty sure I did this last night.
Is it that the development tool doesn't import them? I'm just embedding them directly via actionscript.
Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic