PDA

View Full Version : [AS3] Creating Flash game for kids!


flyhigh
06-26-2009, 03:38 AM
Hi all,

I am new to this forum, came across this as a desperate attempt to get help.

This is my flash code game. There should be random signs appearing. You have to move the hand with the mouse to click on the signs. If you clicked successfully, the sign should disappear, and reappear somewhere else randomly, and you gain 10 points. There is a time limit of 60 seconds. If within 60 seconds you are able to gain 100 points, then you win the game. Else, you lose the game. There will also be a random tomato flying at your own sign that you have to move around with the keyboard! If your sign gets hit by the random tomato, the timer will decrease by 5 seconds, making the time limit shorter.

"CODE"
package campaigngame{

import flash.display.MovieClip;
import flash.events.*;
import flash.ui.Mouse;
import fl.transitions.Tween;
//import fl.transitions.TweenEvent;
import fl.transitions.easing.*;
import flash.ui.Keyboard;
import flash.utils.Timer;
import flash.text.TextField;


public class CampaignGame extends MovieClip {

private var myMoveX:Number=0;
private var myMoveY:Number=0;
//private var mySign:MovieClip;
private var myTimer:Timer;
private var num:Number=10;
private var Score:Number=0;
private var mySignTimer:Timer=null;
private var myTween:Tween;
private var myTomatoe:Tomatoe;



public function CampaignGame() {
var startY:Number=40;
var stopX:Number=900;
var stopY:Number=3000;



stage.addEventListener(MouseEvent.MOUSE_MOVE, moveMe);

hand.addEventListener(MouseEvent.MOUSE_DOWN, pressMe);
hand.buttonMode=true;

stage.addEventListener(KeyboardEvent.KEY_DOWN, myKeyPress);
startBtn.addEventListener(MouseEvent.CLICK, Start);



hand.addEventListener(MouseEvent.CLICK, hitSign);

}
private function Start(e:MouseEvent):void {

insPage.visible=false;
gamePlay.visible=true;
ins1.visible=false;
ins2.visible=false;
sign.visible=false;
ins.visible=false;
startBtn.visible=false;
replay.visible=false;
winGame.visible=false;
loseGame.visible=false;
myTimer=new Timer(1000,10);
myTimer.addEventListener(TimerEvent.TIMER, setClock);
myTimer.addEventListener(TimerEvent.TIMER, animate);
myTimer.start();
myTimer.addEventListener(TimerEvent.TIMER_COMPLETE , Stop);
mySignTimer=new Timer(1000);
mySignTimer.addEventListener(TimerEvent.TIMER,hide Sign);
mySignTimer.start();
replay.addEventListener(MouseEvent.CLICK,Replay);
//myTomatoe.start();
//myTomatoe.visible=true;


}

private function Stop(e:TimerEvent):void {
myTimer.stop();
mySignTimer.stop();
winGame.visible=true;
replay.visible=true;
}

private function Replay(e:MouseEvent):void {
myTimer=new Timer(1000,10);

myTimer.addEventListener(TimerEvent.TIMER, setClock);
myTimer.addEventListener(TimerEvent.TIMER, animate);
myTimer.start();
mySignTimer.start();

replay.visible=false;
winGame.visible=false;


}

private function hitSign() {

if (hand.hitTestObject(Sign1)) {
//myTomatoe=new Tomatoe();
//private function myTomatoe(e:MouseEvent) {
//stage.addEventListener(Event.ENTER_FRAME, animate);

//addChild(myTomatoe);


//var startX:Number=Math.random()*300;
//var StopX:Number=Math.random()*300;
//myTween=new Tween(myTomatoe,"x",Regular.easeInOut,0,750,1,true);
//myTween=new Tween(myTomatoe,"y",Regular.easeInOut,0,400,1,true);
trace("hitting");
Sign1.visible=false;
if (Sign1.visible==true&&Sign1.currentFrame==1) {
//Score=Score+1;
}
Sign1.gotoAndStop(2);



} else {

trace("not hitting");
}

if (hand.hitTestObject(Sign2)) {

trace("hitting");

} else {

trace("not hitting");
}
}

private function hideSign(e:TimerEvent):void {

var randomNum:Number = Math.floor(1 +(Math.random() * (4- 1)));
if (randomNum==1) {
if (Sign1.visible==true) {
Sign1.visible=false;
} else {
Sign1.visible=true;
Sign1.gotoAndStop(1);

}
} else if (randomNum == 2) {
if (Sign2.visible==true) {
Sign2.visible=false;
} else {
Sign2.visible=true;
Sign2.gotoAndStop(1);


}
}
}



private function moveMe(e:MouseEvent) {
hand.x=mouseX;
hand.y=mouseY;
}
private function setClock(e:TimerEvent) {

num--;
trace(num);

myText.text=String(num);
if (num==0) {
trace("done");
myText.alpha=0;

}


}



private function pressMe(e:MouseEvent) {
e.target.startDrag(true);
//stage.addEventListener(MouseEvent.MOUSE_UP, dropMe);

}

private function animate(e:TimerEvent) {

hand.x+=myMoveX;
hand.y+=myMoveY;

}



private function myKeyPress(e:KeyboardEvent) {
trace(+ Keyboard.LEFT);
if (e.keyCode==Keyboard.LEFT) {
mySign.x+=-20;
} else if (e.keyCode == Keyboard.RIGHT) {
mySign.x+=20;

}
}

}

}


BELOW ARE THINGS I NEED HELP WITH

-MAKING SIGNS APPEARING AND DISAPPEARING RANDOMLY (somewhat successful?)
-CLICKING ON THE SIGNS AND ADDING 10 POINTS TO THE TOTAL SCORE
-IF SCORE REACHES 100 OR MORE, A “YOU WIN” MESSAGE SHOWS UP AND THE GAME IS FREEZED. THERE WILL BE A REPLAY BUTTON TO RESTART THE GAME AGAIN
-IF SCORE DOESN’T REACHES 100 OR MORE IN 60 SECONDS, A “YOU LOSE” MESSAGE SHOWS UP AND THE GAME FREEZED. A REPLAY BUTTON TO RESTART THE GAME AGAIN

Thanks in advance!

Darksydaz
06-26-2009, 04:11 AM
i know this might be a little vague... but your first one... you should look up "addChild" to add to the stage dynamically. If you know how to add items to the stage dynamically then addChild...

Library Window > Find Item in library > Right Click > Linkage > give class name

you'll then reuse that class name when you give a variable...

var MyName:(chosen class name here) = new (chosen class name here)
MyName.addchild();

and you can add this to the stage Dynamically... i think that might help better than the method you might be using (although i know very little about writing classes and public and private functions....

2.)You might wanna look into event listeners. specifically (getChildByName.addEventListener(MouseEvent.Mouse_ UP, MyFunction)

3.) you'll need to add event listeners to the stage... stage.addEventListener(Event.Enter_Frame, MyFunction) and trace weither or not the score has been reached... you'll also need TimerEvents... the same thing goes for 4... I hope this helps a little...

flyhigh
06-26-2009, 10:16 AM
Thx for ur suggestions. I am stuck at the hitcheck using hittestobject, it returns me an error
TypeError: Error #2007: Parameter hitTestObject must be non-null.
at flash.display::DisplayObject/_hitTest()
at flash.display::DisplayObject/hitTestObject()
at campaigngame::CampaignGame/hitSign()

any idea how to fix this?

private function hitSign(e:MouseEvent) {


if (hand.hitTestObject(Sign1.hitbox)) {



trace("hitting");
Sign1.visible=false;
stage.removeEventListener(MouseEvent.MOUSE_UP, hitSign);
if (Sign1.visible==true&&Sign1.currentFrame==1) {
//Score=Score+1;
}
Sign1.gotoAndStop(2);



} else {

trace("not hitting");
}

if (hand.hitTestObject(Sign2)) {

trace("hitting");
Sign2.visible=false;
stage.removeEventListener(MouseEvent.MOUSE_UP, hitSign);
if (Sign2.visible==true&&Sign2.currentFrame==1) {
//Score=Score+1;
}
Sign2.gotoAndStop(2);


} else {

trace("not hitting");
}
}

rrh
06-26-2009, 03:26 PM
"Parameter hitTestObject must be non-null" means a variable being passed to a function named hitTestObject was not defined. Throw in some traces to find out which variable is the problem, like this:

trace("sign2 = " + Sign2);
if (hand.hitTestObject(Sign2)) {

flyhigh
06-26-2009, 07:36 PM
still the same problem with both sign 1 and sign 2 with the traces, doesnt give me anything but the problem I said earlier

rrh
06-26-2009, 09:11 PM
If it traces nothing that still gives you information. It gives you the information that the program did not execute the code where the trace was. So try some more traces until you find out what code is being executed, and what line of code is throwing the error.