View Full Version : Why can't AS3 see this?
doomtoo
July 14th, 2009, 11:00 PM
I am trying to add "entities" to an array, that I go through and give updated information each frame.
So I declared the array for the class, in the initialization function I push back a new "BadGuy" into the array, then in the update function I try to move the only entity in the array. The guy doesn't actually move on screen, but tracing out his x shows that it is moving- so somehow the one on the stage isn't associated with the one in the array!
BTW- in the constructor I send the stage as a variable so the badguys can add themselves to the stage when constructed.
Basically:
public class GAME extends MovieClip {
var entities = new Array();
public function InitializeGame():void {
//BadGuy is my custom Class
entities.push(new BadGuy(10, 10, stage));
addEventListener(Event.ENTER_FRAME,onEnterFrame);
}
private function onEnterFrame(event:Event):void
{
entities[0].x++;
}
}
Any ideas?
bluemagica
July 14th, 2009, 11:12 PM
post your badguy code
doomtoo
July 15th, 2009, 12:00 AM
Doh! Thanks, forgotten that I have to move the "image loader" instead of just the object. How would I assign the image to the class so that I could say "badguy1.x=10"? instead of "badguy1.loader.x=10"
public class BadGuy extends MovieClip
{
var imageLoader:Loader = new Loader();
var stage1;
public function BadGuy( x1:int, y1:int, stage)
{
stage1=stage;
this.x=x1;
this.y=y1;
imageLoader.load(new URLRequest("resources/images/monster_basic.png"));
imageLoader.x=200;
imageLoader.y=100;
stage1.addChild(imageLoader);
}
}
doomtoo
July 15th, 2009, 12:39 AM
Figured out a way to do it- I had BadGuy extend Loader instead of MovieClip... which rises a new problem(maybe a new thread)- how do I externally load a set up images/animations and play them?
Like eventually I want to have files
badGuy_walk1.png
badGuy_walk2.png
badGuy_walk3.png
badGuy_fight1.png
badGuy_fight2.png
badGuy_fight3.png
and load them in at run time when needed, and loop through them depending on what is needed?
flyingmonkey456
July 15th, 2009, 12:46 AM
Figured out a way to do it- I had BadGuy extend Loader instead of MovieClip... which rises a new problem(maybe a new thread)- how do I externally load a set up images/animations and play them?
Like eventually I want to have files
badGuy_walk1.png
badGuy_walk2.png
badGuy_walk3.png
badGuy_fight1.png
badGuy_fight2.png
badGuy_fight3.png
and load them in at run time when needed, and loop through them depending on what is needed?
look on google or something for a tutorial on sprite animations. that would take a really long explanation and they're already all over the internet.
doomtoo
July 15th, 2009, 01:32 AM
They do it with loading the images externally? And I had heard Sprites can't have animations so you have to do them as movie clips?
I've done animations with importing from flash in series(and putting each image on a different frame of a movie clip), but not through actionscript.
I'll check it out, but if anyone knows offhand, I'd think it be something like:
class guy extends MovieClip
{
animation_attack:Array = new Array()
current_frame:int;
MovieClip()
{
current_frame=0;
for(var i=0; i<10;i++)
animation_attack:.push(load("attack"+i+".png"));
}
Attack()
{
this.image=animation_attack[current_frame];
if(current_frame<animation_attack.length-1)
current_frame++;
else
current_frame=0;
}
}
bluemagica
July 15th, 2009, 01:44 AM
concept of sprite is a bit different inside and outside of flash! Normally sprite means a graphics that is used in a game, so it can be animated or not......but when it comes to flash, sprite is still a graphic, but restricted to a single frame! What you can do is use a simple trick.....load a sprite-sheet! Its like a filmstrip where one large image is divided in equal rectangles, each containing one picture of the total animation! Next, you bitmapData scrollRect property in flash, to cut out and display the images in order and create the illusion of animation!
But anyway, why are you going through all the trouble of loading pictures externally? c++ ways are different I guess, but even c++ developers most of the time create themselves an editor to save the trouble, and flash is already giving you a graphical IDE and all the tools you need, so why not use those?
Gnoll
July 15th, 2009, 02:08 AM
"and flash is already giving you a graphical IDE and all the tools you need, so why not use those?
" Well Flash IDE sucks :P
Maybe for dynamic loading of images etc? Easy to provide updates if the images are external, most large applications don't have all their images embedded, imo.
If you want to change the x of the loader you could do
public set function x(val:int)
{
loader.x = val;
}
public get function x()
{
return loader.x;
}
doomtoo
July 15th, 2009, 01:11 PM
Thanks! Forgot I had seen that, but haven't used it before, but that is a terrific application of it.
And yeah, for loading externally it will be nice so that I can load dynamically (using filler images for now), but also I am hoping to conserve load time/memory usage by loading things only when needed.
I've been working on a RPG game, and it has the potential of becomming very bloated :-o
Thanks for the help!
Gnoll
July 15th, 2009, 06:58 PM
This can be handy for many applications for adding new methods when you edit a property :) eg. when you have performed set x, set y you could also call your hittest etc
Gnoll
TOdorus
July 16th, 2009, 05:12 AM
Spritesheets are a slight bit better then a series of loose graphics when talking about memory usage. And with slight I mean 95%. It may differ from situation to situation, but still that was enough to convince me. Linky (http://board.flashkit.com/board/showthread.php?t=780658&highlight=flash+memory+limit)
I've posted some more about spritesheets in this thread (http://www.kirupa.com/forum/showthread.php?t=324447) if it might interest you.
doomtoo
July 16th, 2009, 04:52 PM
Awesome, thanks!
I think it might be the time called to load each one. Is there a file format where you could combine all the images in a zip type structure, load the entire thing once, and take out what you need?
I might load all my resources through actionscript since it sucks to have a larger flash file(loading time), and having to update the external images every time you change them.
Right now instead of doing the set/get functions (which might be better), I am just having my objects extend 'loader' instead of 'Sprite'- any disadvantage to this?
SparK_BR
July 17th, 2009, 01:34 AM
it's a shame flash can't read .tga, it's very small
but PNG with a spritesheet and a custom class that can get the right picture you want
like a function to load the frame you want of a certain X by Y hero on a spritesheet
and it can extend loader, so you don't have to pass the loader class to your's before using it.
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.