Ultimate Gear War
Join the alien war, prepare your gear and protect your base at all cost!
4.19 / 5.00 15,550 ViewsMy problem is that I've added an object to the stage(myBalloon), and i can't see it! i've changed it's x,y so that it's in the middle(ish) of the stage, then i moved it to the very front, and i've used a trace to make sure it's there. and even though it says it is, it isn't! this is driving me insane. I'm really hoping someone here can help me...
here's my Game class (and yes, i've made sure that the objects i add after the balloon aren't on top of it)
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class Game extends MovieClip
{
public static var count:int;
private var fruitLimit:int = 0;
private var treeLimit:int = 0;
private var hoverUp:Boolean = false;
public static var hoverTime:int = 0;
private var kRight:Boolean;
private var kLeft:Boolean;
private var kUp:Boolean;
private var kDown:Boolean;
public static var myBalloon:Balloon = new Balloon ;
public function Game()
{
stage.addChild(myBalloon);
if (myBalloon.stage)
{
trace("on stage");
}
else
{
trace(" not on stage");
}
stage.addEventListener(KeyboardEvent.KEY_DOWN,keydown);
stage.addEventListener(KeyboardEvent.KEY_UP,keyup);
addEventListener(Event.ENTER_FRAME,Loop);
}
private function Loop(e:Event):void
{
count++;
time.text = "score: " + count;
if ((count >= fruitLimit))
{
var myFruit:Fruit = new Fruit ;
stage.addChild(myFruit);
myFruit.x = Math.random() * 400;
if ((fruitLimit <= 1800))
{
fruitLimit += 120;
}
else
{
fruitLimit = 1800;
}
}
if ((count >= treeLimit))
{
var myTree:Trees = new Trees ;
stage.addChild(myTree);
var i:int;
i = Math.random() * 2;
if ((i > 1))
{
myTree.x = 400;
}
else
{
myTree.x = 0;
}
if ((treeLimit < 600))
{
treeLimit += 120;
}
else if ((treeLimit >= 600))
{
treeLimit = 600;
}
}
if (kUp)
{
myBalloon.y -= 3;
}
if (kDown)
{
myBalloon.y += 3;
}
if (kLeft)
{
myBalloon.x -= 3;
}
if (kRight)
{
myBalloon.x += 3;
}
if (myBalloon.x < 0)
{
myBalloon.x = 0;
}
if (myBalloon.x > 400)
{
myBalloon.x = 400;
}
hoverTime++;
if ((hoverTime >= 0))
{
if ((hoverTime >= 72))
{
hoverTime = 0;
if (hoverUp)
{
hoverUp = false;
}
else
{
hoverUp = true;
}
}
if (hoverUp)
{
myBalloon.y++;
}
else
{
myBalloon.y--;
}
}
}
private function keydown(k:KeyboardEvent)
{
if (k.keyCode == 37)
{
kLeft = true;
}
if (k.keyCode == 39)
{
kRight = true;
}
if (k.keyCode == 38)
{
kUp = true;
}
if (k.keyCode == 40)
{
kDown = true;
}
}
private function keyup(k:KeyboardEvent)
{
if (k.keyCode == 37)
{
kLeft = false;
}
if (k.keyCode == 39)
{
kRight = false;
}
if (k.keyCode == 38)
{
kUp = false;
}
if (k.keyCode == 40)
{
kDown = false;
}
}
}
}
Here's the Balloon class:
package {
import flash.display.MovieClip;
import flash.events.Event;
public class Balloon extends MovieClip {
public function Balloon() {
addEventListener(Event.ENTER_FRAME, balloonLoop);
this.y = 550;
this.x = 200;
}
private function balloonLoop(e:Event):void{
this.parent.setChildIndex(this, this.parent.numChildren-1);
}
}
} If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.
unit test the ever-living hell out of it. Start with something obvious that you know will work, like addChilding a simple sprite to the class, then work your way up from there. That's how you'll find the break.
Programming stuffs (tutorials and extras)
PM me (instead of MintPaw) if you're confuzzled.
thank Skaren for the sig :P
At 1/20/13 10:08 PM, egg82 wrote: unit test the ever-living hell out of it. Start with something obvious that you know will work, like addChilding a simple sprite to the class, then work your way up from there. That's how you'll find the break.
*sigh*...I guess that'll be the only way. I was hoping there was some dumb problem i over looked and someone would just be like, "Oh you just forgot *insert thing i forgot here*". Oh well, at least this is a tiny project. thanks for the reply
If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.
It looks all good from here, but here's a few tips.
"public static var"
Never, never use static.
if (kLeft)
{
myBalloon.x -= 3;
}
if (kRight)
{
myBalloon.x += 3;
}
Why not:
if (kLeft) myBalloon.x -= 3;
if (kRight) myBalloon.x += 3;
if (hoverUp)
{
hoverUp = false;
}
else
{
hoverUp = true;
}
To flip a boolean use
hoverUp = !hoverUp
"private function keydown(k:KeyboardEvent)"
Typecast your function, you should actually receive a warning for not doing this, a possible reason for not doing this is using the animation IDE Flash CS, if so, don't do this, use a coding IDE, I suggest FlashDevelop but other do exist such as FlashBuilder.
Oh and one more thing, never write logic in the constructor, if an error occurs in it then it cannot be debugged.
At 1/21/13 01:55 AM, MintPaw wrote: Oh and one more thing, never write logic in the constructor, if an error occurs in it then it cannot be debugged.
FD will set you up with a default Main with the lines
if(stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
in the constructor.
just change it to
addEventListener(Event.ADDED_TO_STAGE, init);
which will prevent you from writing code coming from the constructor, since events break the flow of code.
Programming stuffs (tutorials and extras)
PM me (instead of MintPaw) if you're confuzzled.
thank Skaren for the sig :P
At 1/21/13 01:53 AM, MintPaw wrote: It looks all good from here, but here's a few tips.
"public static var"
Never, never use static.
Bit of a bold statement.
At 1/21/13 03:02 PM, Sam wrote:At 1/21/13 01:53 AM, MintPaw wrote: It looks all good from here, but here's a few tips.Bit of a bold statement.
"public static var"
Never, never use static.
Never!
Well, I mean maybe static helper methods like Math.abs() and junk, but never integrate it into your game as a value the game relies on or anything.
At 1/21/13 07:25 PM, MintPaw wrote:At 1/21/13 03:02 PM, Sam wrote:Never!At 1/21/13 01:53 AM, MintPaw wrote: It looks all good from here, but here's a few tips.Bit of a bold statement.
"public static var"
Never, never use static.
Well, I mean maybe static helper methods like Math.abs() and junk, but never integrate it into your game as a value the game relies on or anything.
It's often misused but I wouldn't say "never". It's a powerful feature in a language, but the keyword seems to get tossed around for the wrong reasons.
I try to answer to your question, did you've setted the Balloon Class as a BaseClass or just as a Class in Flash for your Balloon Graphic? In the Case you're using you have to place it in the "Class" field Otherwise you only create an Instance of The Clas without an assigned Graphic/Movieclip
I code Flash Games! Check out the Stealth Pervert and VOID
At 1/21/13 01:53 AM, MintPaw wrote: "public static var"
Never, never use static.
Never say never.
Static variables are good for storing colour codes like this:
public class Color {
static public const RED:int = 0xFFFF0000;
static public const GREEN:int = 0xFF00FF00;
static public const BLUE:int = 0xFF0000FF;
static public const YELLOW:int = 0xFFFFFF00;
static public const PURPLE:int = 0xFFFF00FF;
static public const CYAN:int = 0xFF00FFFF;
}
And anything else that you'll only ever have one of (such as the speed of light in a vacuum, should your game/app require it).
At 1/22/13 06:06 AM, Diki wrote:At 1/21/13 01:53 AM, MintPaw wrote: "public static var"Never say never.
Never, never use static.
Static variables are good for storing colour codes like this:
public class Color {
static public const RED:int = 0xFFFF0000;
static public const GREEN:int = 0xFF00FF00;
static public const BLUE:int = 0xFF0000FF;
static public const YELLOW:int = 0xFFFFFF00;
static public const PURPLE:int = 0xFFFF00FF;
static public const CYAN:int = 0xFF00FFFF;
}
And anything else that you'll only ever have one of (such as the speed of light in a vacuum, should your game/app require it).
Static constants for the height, width, half height and half width are also really useful. Saves the time it takes to look up stage.stageWidth, and the calculation to get half of that.
At 1/22/13 11:15 AM, Sam wrote: Static constants for the height, width, half height and half width are also really useful. Saves the time it takes to look up stage.stageWidth, and the calculation to get half of that.
i'm much less comfortable with that. The stage's size can change, you know.
Programming stuffs (tutorials and extras)
PM me (instead of MintPaw) if you're confuzzled.
thank Skaren for the sig :P
At 1/22/13 12:00 PM, egg82 wrote:At 1/22/13 11:15 AM, Sam wrote: Static constants for the height, width, half height and half width are also really useful. Saves the time it takes to look up stage.stageWidth, and the calculation to get half of that.i'm much less comfortable with that. The stage's size can change, you know.
In fullscreen scenarios and with certain scale modes set, yes. But in any application that's built the constants you may use change depending on what is and isn't going to happen. I'm not always going to need constants for colours. It just so happens that the applications I build rarely have need for fullscreen, and the constants lend themselves really well.
Anything for a bit more performance, eh?
At 1/22/13 12:14 PM, Sam wrote: In fullscreen scenarios and with certain scale modes set, yes. But in any application that's built the constants you may use change depending on what is and isn't going to happen. I'm not always going to need constants for colours. It just so happens that the applications I build rarely have need for fullscreen, and the constants lend themselves really well.
Anything for a bit more performance, eh?
if they open your swf in a flash player exe, they can resize the window. If it's AIR, it's automatically put in an exe. I like to account for as many variables as I can.
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
:3
Programming stuffs (tutorials and extras)
PM me (instead of MintPaw) if you're confuzzled.
thank Skaren for the sig :P