object self destruction?
Hello Community,
(If you have no time for backstory and explainations scroll down to "My Problem", but it would be helpful to understand my problem if you read it and I would be very thankful if you do)
First of all I'm a Actionscript newbie and I want to learn the basics of AS3 game programming (Im also new to the forum, so sorry if I do anything wrong. Also english is my second language, so some formulations may not be correct... sorry). I know general coding basics from school and I want to develop a mini flash game as IT-Class project. The problem is that we only programmed buissness/economy applications and stuff like this with C++ and java. We also went threw object oriented programming, but in game development to me everything ist very different. So if you have good books, tutorials, websites, videos etc. to recomment that would be very helpful.
At the moment I am able to "get things working... somehow" but it isn't very elegant or system friendly.
The Game:
It's a minigame (endless runner) where you try to defend a human butt from bloodsucking flies. They fly, land and start draining. You have to survive and loose when your blood-level is 0.
How it works:
the Flies spawn in the off (around the stage) at a random coordinate. then they get a target coordinate on the landing plane (Le human butt
) and turn to that point. they fly there, land and start draining. The user can hit them from the moment they enter the stage. If the user hits them in the air, they just explode, if he hits them after landing, they splatter. if they drain, your blood level decreases, the flies get bigger and the butt color goes from a healthy pink to a vampirish white. You have to survive as long as possible. The flies become faster and more the longer you play.
I want to add some special abilities later on (like time freeze for 10 sec. or an exploding fart that kills all flies on stage etc.), but thats pie in the sky at the moment. (don't judge me for my humor haha
)
What I've done:
- half of the art/animation part (butt, life-bar, and kill animations missing)
- I use the TweenLite engine for my animations
- 1 Main class with all the game logic (tried to be object oriented)
- a Fly class with all the functionality of those suckers
- a Blood class with the bloodlevel Var linked to a textbox (As I said, no bar yet)
the game work's, but I want a good class structure and system. I imagine it like legos. One core brick with the basic functionality that i can expand with smaller bricks, like the mentioned special abilities etc.

Le Game


Le Flies doing Fly stuff Le Fly draining blood
My Problem:
I dont always know what belongs to wich class. It's some kind of general problem but this specific case is very annoying:
I spawn the fly objects with a timer in the main class. First I tried to do keep control over them by putting every fly by spawning in an Array. I thought like this I am able to remove the object with my main class if the user clicks it, just like it was created... It worked, but only with the first fly, because if you removed for example [object fly] on Arrayfield 43 the flies on field 44, 45 etc. budge up to close the gap in the Array. Thats why you lose control after a while and if you click one fly, sometimes a random other fly anywhere on the screen dies
. I needed an Array thats not budging up but would leave a empty placeholder on field 43 till the next fly spawns and closes that gap.
Long story short: I didn't find a way to keep control over them via the main class and after hours of frustration I wrote a kill function inside the fly class. Like this the object destroys itself...
public function kill(event:MouseEvent):void
{
suckTimer.stop();
TweenLite.killTweensOf(this);
this.removeEventListener(MouseEvent.MOUSE_DOWN, kill);
suckTimer.removeEventListener(TimerEvent.TIMER, drain);
parent.removeChild(this);
}
It works now, but I think thats not a nice way to do... what do you think? I am willing to redo all of my classes with your help. I really want a nice performing and structured code
2 Questions:
-Does my solution at the moment leave any unwanted rests of the object, that influence the performance after a while and do you know a way to check that besides using the trace function?
-Do you know an elegant solution for my problem? I can imagine that there is an obvious way a experienced programmer would do it, but i have no idea.
As I said, I also would be very happy about general advices and recommendations (books, tutorials etc.) so I dont have to annoy you haha ![]()
A very very big thanks to every help I get
PS: I would like to add the class ( .as ) files for those who want to read them and give tips on style and structure. Is this somehow possible?
