Monster Racer Rush
Select between 5 monster racers, upgrade your monster skill and win the competition!
4.23 / 5.00 3,881 ViewsBuild and Base
Build most powerful forces, unleash hordes of monster and control your soldiers!
3.93 / 5.00 4,634 ViewsWould help if you had a website to showcase something, anything, to show it's a real project - let alone not a scam. Just an idea.
As the saying goes...if you find yourself repeating code there must be a better way to go about it....
In this class you will see how the way I am going about this will start to get very redundant... Game starts out with items already on the stage in a container. I add them to an array and loop through them for hit detection. But...after that level ends, I reset the level - add elements back into array - and loop again. You'll see what I mean by redundancy in my code.
I have 10 levels/stages (_lvl...) in my game so I want to find a better way to go about this. Having to do a new var for each loop to fill the array doesn't feel right.
package
{
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.events.MouseEvent;
import flash.ui.Mouse;
import flash.events.Event;
public class Level extends MovieClip
{
private var _lvl:Lvl;
private var _lvl2:Lvl2;
private var _boneArray:Array;
private var _stage1:Boolean;
private var _stage1Complete:Boolean;
private var _stage2:Boolean;
private var _stage2Complete:Boolean;
private var _stage3:Boolean;
public function Level(dC)
{
_lvl = new Lvl();
_lvl2 = new Lvl2();
_stage1 = true;
dC.addChild(_lvl);
_boneArray = new Array();
//Add bones childs to array
var a:Number = 0;
while (a < _lvl.p1.numChildren)
{
_boneArray.push(_lvl.p1.getChildAt(a));
a++;
}
}
public function resetLevel(_player, dC):void
{
if (_stage1Complete)
{
dC.setChildIndex(_player, 2);
dC.addChildAt(_lvl2, 1);
dC.removeChild(_lvl);
var b:Number = 0;
while (b < _lvl2.p2.numChildren)
{
_boneArray.push(_lvl2.p2.getChildAt(b));
b++;
if (_boneArray.length == 5)
{
_stage2 = true;
_stage1Complete = false;
}
}
}
if (_stage2Complete)
{
//Repeat as above...
}
}
public function lvlUpdate(_player, dC):void
{
if (_stage1)
{
for (var i:Number = 0; i < _boneArray.length; i++)
{
if (_player.hitTestObject(_boneArray[i]))
{
_lvl.p1.removeChild(_boneArray[i]);
_boneArray.splice(i, 1);
}
if (_boneArray.length == 0)
{
_stage1 = false;
_stage1Complete = true;
resetLevel(_player, dC);
break;
}
}
}
if (_stage2)
{
for (i = 0; i < _boneArray.length; i++)
{
if (_player.hitTestObject(_boneArray[i]))
{
_lvl2.p2.removeChild(_boneArray[i]);
_boneArray.splice(i, 1);
}
if (_boneArray.length == 0)
{
_stage2 = false;
_stage2Complete = true;
resetLevel(_player, dC);
break;
}
}
}
if (_stage3)
{
//Repeat as above...
}
}
}
}
Now I could keep going on this way and finish my game as there is only 10 levels. But that means I'm being redundant 10 times. While a finished game would be nice, I am more concerned with learning better code practices as this (programming) is something I really want to learn well.
Are you talking about a movieclip with frames inside? then you animate it on main timeline? Because if so, even if you kept that movie clip on first frame of main timeline it would play all of it's frames regardless - unless you use actionscript and put a stop(); inside it's frames.
The API uses SO if you're not on NG and logged in.
Sorry, but just to be clear, if I were to use the NG API and then host the game to other websites it would still save for the player? It would just save locally on the user's computer?
My current skills make me kind of far from making a game that will need a save feature, but I just thought I'd ask now for future reference.
@MintPaw I will do - may be awhile before it is done haha
@milchreis You know since I started learning at the start of the year a lot of people, including yourself, would post links in my post to read the actionscript API webpage. I often tried but it was difficult to understand and I think I disliked the website because to me it read like a boring instruction manual haha
But maybe I'm just improving after all this time. I read everything in the link you sent me and I actually grasped most of it. I started having other issues with my display container....so I went back to the actionscript API webpage and, I have to say, I see it a whole new way now. I really like it now. Using your advice, and MintPaw's, I was able to read the actionscript API and find a solution to my problem. I can't believe I now know how to pass an object as a parameter now!
Just thought I would share this with you guys. I know you have been at this for quite some time now so this kind of code is probably extremely basic to you guys, but I can't help but feel excited that I've learned this and expanded my skill set (however small that may be...).
Thank you.
It's hard to break problems into smaller problems if you don't know the components of the problem.
Ain't that the hard truth :P
Thank you for the clarity on the subject. I'll keep at it. Also, Mintpaw, you probably don't care but I just wanted to let you know that your game "The great heist" that you made back in August of 2009 inspired one of my current projects that I'm working on haha
Bet you thought something you made almost 5 years ago would have that effect on someone :P
Thank you for the input. It was kind of refreshing to understand everything you suggested. Guess I'm starting to catch on with everything I've been learning over these months. One thing I need to read more about though is something you mentioned here...
Take the _boneArray and move it into the Lvl class.
The entire for loop of hittesting would also go into that class, into some dedicated hittesting method you have yet to create.
pass the player object as a parameter.
You would call this method everytime you want to hittest, just like the "udate" function of player.
The "pass the player object as a parameter. I see this done in tutorials and people here in these forums posting such things with their code. The arguments in all my functions have been empty up until this point. Well, other than say (event:Event) and other simple ones that usually go with standard event listener's such as a mouse event handler.
I just found the Function Parameters page in the actionscript API website so hopefully that will help me understand it's usefulness and exactly how it works.
Looks like flash drops the quality pretty dramatically when i compress. Can i do this in southbooth and only drop the quality by a little?
Are you choosing the right format when compressing it in Flash? Because after you choose the format type, you can choose the level of compression. With each increment of compression you can then test the audio right there in the dialogue box to hear how it sounds.
I believe you can do the same thing in soundbooth. Not too familiar with that program.
I'm still learning Arrays and loops. I've studied tutorials so I can see them implemented in games and I've gone over the actionscript API a few times...
I just want to make sure I'm going about this the right way. I've been at it for days trying to make this work and I've finally got it working, but that doesn't necessarily mean I'm doing it correctly.
I have a level container (_lvl) that I add to the stage. Inside that is a container (p1) for holding items I want the player to collect - in this case bones. I didn't add the bones dynamically to the p1 container so I had to give all 5 of them instance names - b1, b2.....b5 as you will see in the array.
package
{
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.events.MouseEvent;
import flash.ui.Mouse;
import flash.events.Event;
public class GameManager extends MovieClip
{
private var _player:Player;
private var _bone:Bone;
private var _bush:Bush;
private var _lvl:Lvl;
public var _boneArray:Array;
private var _bushArray:Array;
public function GameManager()
{
addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}
private function onAddedToStage(event:Event):void
{
init();
addEventListener(Event.ENTER_FRAME, loop);
}
function init():void
{
_lvl = new Lvl();
addChild(_lvl);
_player = new Player();
addChild(_player);
_boneArray = new Array(_lvl.p1.b0,_lvl.p1.b1,_lvl.p1.b2,_lvl.p1.b3,_lvl.p1.b4);
}
public function loop(event:Event):void
{
_player.update();
for (var i:Number = 0; i < _boneArray.length; i++)
{
if (_player.hitTestObject(_boneArray[i]))
{
_lvl.p1.removeChild(_boneArray[i]);
_boneArray.splice(i, 1);
}
}
}
}
}
Am I going about this the right way? I'm sure there is something in here that you upper-level programmers could tear into me about haha.
Thanks in advance for any advice, tips, or tricks.
At 7/24/14 03:28 PM, EmeyeX wrote: I'm working on an animation thats about 8mb, and i noticed it takes a while to load. Idk if its my computer that takes forever to load it or what. So im wondering what do people consider too big of a file.
Its the music in my animation thats using it all up.
Compress your music file. You can even do it inside flash if you don't have an audio program.
At 7/23/14 08:13 AM, Varjokani wrote: I was thinking maybe I should add some adds to my flash game to get money irl. What would you recommend me to do? :3
Yes, Im newbie.
You could use newgrounds ads and integrate them into your project. Just know that there is hardly any money in it unless you make a hit game and even then it's barely anthing. Make games because you have a passion for it - any ad revenue, no matter how tiny is, is just an added plus.
to add to this: they don't have to be added to the stage
Only the InteractiveObject with focus will receive the Keyboard events.
But all of them will end up at the stage because they bubble up the display list, so adding the listener to the stage is a good idea.
I did not know this. I played a flash game recently where you could switch between characters yet still use the same controls for them. I am guessing that they did this by switching focus on current character selected (as well as other code).
Your Player class relies on the stage in its update function to restrict its movement.
Therefore, the update function can only be called if the Player object is on the display list (not being on the list makes its stage property null and thus causes a #1009 Error), just FYI
Finally grasped that #1009 error. Can see how that is a pain in the ass to beginners. However, it did not cross my mind how it could call that error due to my update function if player isn't on the display list. Thank you for the tip.
if (e.keyCode == 37) {
player.moveLeft(); // player.moveBy(-1, 0); etc
}
Key functions should be in GameManager because your player might not be the only thing in the entire game that requires key presses.
Thank you. I will have to rework my code. I realized why it worked in my other game is because my Player class in that game had an addEventListener(Event.ADDED_TO_STAGE, onAddedToStage). I tried it with this current game and it works now. But I will rework my code the way you suggested for practice.
Key listeners have to be added to the stage, and that should be done from your GameManager class.
Weird since they worked in my other game..
how then do I access the key handler functions in the Player class?
I'm missing something simple here I know it. I used this exact same setup up for a different game and it worked. Only thing different is that I learned to only have one ENTER_FRAME in my code so this time I ran an update for the Player class instead of having that class use its own ENTER_FRAME.
The loop in the GameManager class traces fine. The update function in the Player class traces fine. But the handles for the key controls in the Player class do not trace.
GameManager class:
package
{
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.events.MouseEvent;
import flash.ui.Mouse;
import flash.events.Event;
public class GameManager extends MovieClip
{
private var _player:Player;
private var _bone:Bone;
private var _bush:Bush;
private var _lvl:Lvl;
private var _boneArray:Array;
private var _bushArray:Array;
public function GameManager()
{
addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}
private function onAddedToStage(event:Event):void
{
_lvl = new Lvl();
addChild(_lvl);
_player = new Player();
addChild(_player);
addEventListener(Event.ENTER_FRAME, loop);
}
function loop(event:Event):void
{
_player.update();
}
}
}
Player Class
package
{
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.events.Event;
public class Player extends MovieClip
{
private var _vx:int;
private var _vy:int;
private var _playerHalfWidth:uint;
private var _playerHalfHeight:uint;
private var _leftKey:Boolean;
private var _rightKey:Boolean;
private var _upKey:Boolean;
private var _downKey:Boolean;
public function Player()
{
resetPlayer();
_playerHalfWidth = width / 2;
_playerHalfHeight = height / 2;
_vx = 0;
_vy = 0;
_leftKey = false;
_rightKey = false;
_upKey = false;
_downKey = false;
//Add stage event listeners
addEventListener(KeyboardEvent.KEY_DOWN,onKeyDown);
addEventListener(KeyboardEvent.KEY_UP,onKeyUp);
}
private function onKeyDown(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.LEFT)
{
_leftKey = true;
}
else if (event.keyCode == Keyboard.RIGHT)
{
_rightKey = true;
}
else if (event.keyCode == Keyboard.UP)
{
_upKey = true
}
else if (event.keyCode == Keyboard.DOWN)
{
_downKey = true;
}
}
private function onKeyUp(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.LEFT)
{
_leftKey = false;
}
if (event.keyCode == Keyboard.RIGHT)
{
_rightKey = false;
}
if (event.keyCode == Keyboard.UP)
{
_upKey = false;
}
if (event.keyCode == Keyboard.DOWN)
{
_downKey = false;
}
}
public function update(event:Event):void
{
//Move the player
x += _vx;
y += _vy;
_vx = 0;
_vy = 0;
if (_leftKey)
{
_vx = -5;
}
if (_rightKey)
{
_vx = 5;
}
if (_upKey)
{
_vy = -5;
}
if (_downKey)
{
_vy = 5;
}
//Stop player at the stage edges
if (x + _playerHalfWidth > stage.stageWidth)
{
x = stage.stageWidth - _playerHalfWidth;
}
else if (x - _playerHalfWidth < 0)
{
x = 0 + _playerHalfWidth;
}
if (y - _playerHalfHeight < 0)
{
y = 0 + _playerHalfHeight;
}
else if (y + _playerHalfHeight > stage.stageHeight)
{
y = stage.stageHeight - _playerHalfHeight;
}
}
public function resetPlayer():void
{
x = 320;
y = 240;
_vx = 0;
_vy = 0;
}
}
}
I know I must be overlooking something simple as this code is identical to another game I've made with the exception of using a singe Enter_Frame and using an update function for player.
Was brainstorming simple game ideas and one I cam up with has an aspect to it that I have no idea how to go about coding it.
I want lightning bolts to strike down on the stage. Now I that I would need to use array(s) and loop(s). But I barely grasp how to use them. Only recently learned how to put objects that are already on the stage into them to test for hit detection.
I read tons of tutorials before I figured that much out but I do remember seeing a shit ton tutorials for using Arrays and Loops to spawn enemies at random (or make bricks fall from the sky at random spots).
Using that method I just mention would work if I wanted random lightning strikes, but what if I had a level (or sequence) planned out. What if I knew the order (and location) in which I wanted lightning strikes to come down - how would I go about coding such a thing?
Like....Imagine the stage as if it had 8 columns......and the # after the bolt was the column it was going to fall from...
bolt = new array(bolt1, bolt3, bolt8, bolt5....)
With this example - how in the heck would tell what bolt1 even was? Would it be separate functions that get called from the array like bolt1 would be a function that would addChild a bolt in that first column? If it's not a function then how do I store that kind of info in each bolt?
I'm going to stop writing now in case all of this isn't making any sense to you guys. I'm hoping I got my point across with what I'm trying to do and what I don't understand.
As you can obviously tell I'm very new to arrays and loops....so any help would be greatly appreciated. Any quick pseudo code would be immensely appreciated a well.
At 7/15/14 05:47 AM, FiendMachine wrote: When I use Stream sound in flash,the quality gets reduced but the size of the exported swf is also small.
If I use Event then the sound remains the same but the swf's size is increased by the size of the audio/song file.
I don't want a 1 mb swf to become a 4.5 mb file just because of a song in it.Is there any way I can reduce its size without affecting the quality?
You can compress the song. The more you compress it the lower the quality but you can compress it pretty damn far before it starts to sound noticeably bad. You can either compress it in a program outside of flash for better control like Adobe Audition. If you don't know much about how sound works and all the theory forget that. Just compress it inside Flash. I think you do this by clicking on the sound file properties. From there you can choose what format to compress it in and how much to compress it. Good thing is that you can test how it will sound at a compression rate so you can find the best compression for quality.
Also,many times when I use both Stream and Event sounds in a swf,sometimes a sound plays by itself when I click a button.Solution?
No idea what you mean here. Do you have the sound linked to the button?
For all my other projects though, I follow the (here's a long one) 'reverse domain name package naming convention' (take your website and reverse the domain so google.com becomes com.google.packagename). That is usually a Java thing and most people don't adhere to it in AS3. Nothing too major, it's just a way to organize bits of code in a big project (or just to keep classes organized and easy to find).
Whoa thank you so much for the long, in-depth reply it really helps a lot. That really helps clear things up for me. Thanks again it's much appreciated.
At 7/7/14 01:26 AM, slugrail wrote: import LvlCheck;
Not needed
Thank you slugrail. Is there ever a reason to import a class inside another class?
You should only have 1 enter frame event in your main class. Give your Level class an update() or something that you call each frame from the main class. At 30 fps, each enterframe you add is creating 30 new event objects per second, and it only needs to be done once unless you know what you're doing.
Ooooh now it all clicks. I get it now. I see why people use an update functions. I kind of figured that having more than one EnterFrame was just as intensive as calling a function (like an update function) every frame. Thank you for this.
Trying to change the value of a boolean in another class.
Main class:
package
{
import flash.display.MovieClip;
import flash.events.Event;
import LvlCheck; // I imported the class that has the booleans
public class GameManager extends MovieClip
{
public var lvlCheck:LvlCheck; //declared other class
public function GameManager()
{
addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}
private function onAddedToStage(event:Event):void
{
lvlCheck = new LvlCheck();
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function onEnterFrame(event:Event):void
{
if (!_gotJewel)
{
if (_player.hitTestObject(_jewel))
{
_jewel.hideGem();
_gotJewel = true;
trace("got jewel");
}
}
if (_gotJewel)
{
if (_player.hitTestObject(_spawn))
{
_gotJewel = false;
resetLevel();
trace("reset");
lvlCheck.lvl01 = true; //HERE IS WHERE I SET IT TO TRUE
}
}
}
}
I stripped most of the irrelevant code to make it easier to read. Everything in the EnterFrame handler is just so that when the player grabs the jewel and touches exit you "win" and this is where I want it to change the boolean to true.
Here is the other class it is trying to change the boolean in:
package
{
import flash.display.Sprite;
import flash.events.Event;
public class LvlCheck extends Sprite
{
public var lvl01:Boolean;
public function LvlCheck()
{
addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}
public function onAddedToStage(event:Event):void
{
lvl01 = false;
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
public function onEnterFrame(event:Event):void
{
if (lvl01)
{
trace("lvl1 active");
}
}
}
}
I plan to eventually put the level booleans in an array but first I want to figure this out as it is more simple to me (still learning arrays/loops).
Game worked fine before adding in this new code. It works now. But the trace function in the LvlCheck class doesn't fire in it's EnterFrame handler.
What am I missing... Guess I'm still rusty in understanding how classes communicate to one another....
You usually stick those things into an array. Variables can't start with numbers but you can have them anywhere else in the name.
Thank you. I spent weeks trying to understand loops and arrays. Finally started to grasp it in my current little game by sticking all my walls into an array and using a loop to check for collision. I created this little game to get used to arrays and loops and I do plan on figuring out how to put my level booleans into an array.
Thanks again.
Is it okay to have numbers when declaring variables? I have 10 booleans for levels in the game that go lvlOne, lvlTwo, lvlThree....etc.
Was curious if it was ok to do same concept but with numbers instead: lvl1, lvl2, lvl3 or lvl01, lvl 02, lvl03...
I know you're not suppose to start variables with numbers but is it okay to do this?
No idea... You should probably contact Adobe about this. Also, is this the first time you've ever attempted to download a Flash trial? Like I know you've tried many times to get it to work but in the past have you successfully downloaded a Flash trial? If so that could be the issue.
So, if theres any Flash expert artists who can do 2.5D images (characters, and buildings for the most part, maybe some furniture and rooms and interiors and what not) who want to help, just give me a shout =)
Try copy/pasting this post in the collab forum and the art forum. You may get more replies there :)
At 6/23/14 10:49 PM, MSGhero wrote:At 6/23/14 10:00 PM, Barzona wrote: So, what's the display list problem here? How can I remove these objects when the gameContainer is removed? I'm sure the exact same thing is happening with the hubContainer that holds the overworld maps when it gets removed.container.removeChildren();
Removing a thing doesn't remove it's kids.
I've never seen removeChildren() before. What's the difference between doing
container.removeChildren();
and
container.removeChild();
???
I thought they did the same thing but I guess not...
I have taken that into consideration , I am just going to wait, they might respond here.Maybe word of mouth kind of thing.
And I know artists check this forum out as well ,because many of them use flash.
You'll never get anywhere being this stubborn. You could easily copy and paste the same text in this thread into a new thread in the other forum and then just wait on both.
I actually like it. I like the fact that it's more than just dodging things falling like a lot of dodge games - you have to dodge the effect after it hits the ground. The fact that each type of fruit has a specific way it "splatters" makes one anticipate quickly how they will go about dodging it and the other fruit (if you had the way each fruit splatters random it would be quite frustrating).
You need a much slower build up unless you're going to give the player a lot of HP. Would be neat if like Pacman there was a power-up that would allow you to eat some fruit which could get you out of a sticky situation. There are tons of creative ways you could make this into a really fun/enjoyable game. Keep at it.
I can't really help you for two reasons: I don't know AS2 and your workflow. There are some people that know AS2 so they may be able to help you....but until then....
I know you'll most likely keep it this way for this project but with future games don't work with scenes - or frames for that matter. Your entire game should only be on the very first frame and that's it.
Since you're so new to ActionScript I highly advise you drop AS2 and start learning AS3. It can be difficult at first to learn but after a few months of working at it it opens up so many possibilities for your games. I think I remember you mentioning in a previous post that you were gonna learn more stuff for your current game over summer - why not use that time to dive into AS3. There are some really good beginner books out there to get you started as well as the vast amount of tutorials on the web. Seriously, make the move to AS3.
**About the hitboxes, is it possible to edit that graphically in any way, instead of doing it with AS2? I'm not great at Flash even though I've had it for 4 years... Sorry if that's a dumb question lol**
You could graphically define a smaller hitbox. I'm assuming you are using hitTestObject which is why it's so easy to get hit by by something cuz your character has his pincers sticking far out. So if you wanted a quick graphical solution you could draw a rectangle that is the size of the crab's body, place it inside the crabs MC (just guessing the crab is a movieclip), set the alpha to 0, and give it an instance name like crabHitBox (something shorter would be better). Then you could just reference it like:
if (crab.crabHitBox.hitTestObject(enemy))
{
//do stuff like lose hp
}
And also, do you know if doing the thing with the turtles you suggested is simple, by making the character move with the turtle? Would it be easy to find how to write it online, or would I need pretty good AS experience? I'm going to try to learn it this summer.
It is definitely something you could find online. I don't know the code exactly myself as I'm still learning but I've read it before. It's rather simple so you grasp the code when you read it. You essentially would check if the crab is touching the turtle and if it is then have the crab match the turtle's movement. Try searching the web for "player stick to platform" or something along those lines.