Home Tutorials Forums Articles Blogs Movies Library Employment Press

Go Back   ActionScript.org Forums > General > Gaming and Game Development

Reply
 
Thread Tools Rate Thread Display Modes
Old 04-08-2012, 01:01 PM   #1
cooldude2000
Registered User
 
Join Date: Mar 2012
Posts: 36
Default [AS3] Game help

this is my game ...... there are 9 targets moving randomly (via code) and when you click them they disappear and you get 5 points...
i made a menu btn and when you click the btn it goes to the main menu........
if you click start it starts a new game.... so i want it to continue from where you quit ....
can you help me continue from where you quit ???



THIS IS MY CODE:


var array:Array =
[
target,
target1,
target2,
target3,
target4,
target5,
target6,
target7,
target8
];


var velX:Number = 8;
var velY:Number = 8;



init();

function init():void
{
for each (var b:MovieClip in array)
{
b.addEventListener(MouseEvent.CLICK, ballClick);
b.buttonMode = true;
b.mouseChildren = false;
}
resume_btn.addEventListener(MouseEvent.CLICK, resumeClick);
stop_btn.addEventListener(MouseEvent.CLICK, pauseClick);
stage.addEventListener(Event.ENTER_FRAME, updateBall);
}

function ballClick(e:MouseEvent):void
{
var b:MovieClip = e.target as MovieClip;
removeChild(b);
array.splice(array.indexOf(b), 1);
trace(array.length);
}

function pauseClick(e:MouseEvent):void
{
stage.removeEventListener(Event.ENTER_FRAME, updateBall);
}

function resumeClick(e:MouseEvent):void {
stage.addEventListener(Event.ENTER_FRAME, updateBall);
}
function updateBall(event:Event):void
{
for each (var t:MovieClip in array)
{
t.x += velX;
t.y += velY;

if (t.x > stage.stageWidth - t.width / 2 || t.x < 0 + t.width / 2)
{
velX *= -1;

}
else if (t.y > stage.stageHeight - t.height / 2 || t.y < 0 + t.height / 2)
{
velY *= -1;
}
}
}

var score:uint;

function init10():void
{
score = 0;
scorecounter.text = score.toString();
clip.buttonMode = true;
target.addEventListener(MouseEvent.CLICK, on_press);
}

function on_press(event:MouseEvent):void
{
updateScore();
}

function updateScore():void
{
score += 5;
scorecounter.text = score.toString();
}

init();



var score1:uint;



clip.buttonMode = true;
target1.addEventListener(MouseEvent.CLICK, on_press1);

function on_press1(event:MouseEvent):void
{
updateScore1();
}

function updateScore1():void
{
score += 5;
scorecounter.text = score.toString();
}

init();


var score2:uint;


clip.buttonMode = true;
target2.addEventListener(MouseEvent.CLICK, on_press2);

function on_press2(event:MouseEvent):void
{
updateScore2();
}

function updateScore2():void
{
score += 5;
scorecounter.text = score.toString();
}

init();

var score3:uint;

clip.buttonMode = true;
target3.addEventListener(MouseEvent.CLICK, on_press3);


function on_press3(event:MouseEvent):void
{
updateScore3();
}

function updateScore3():void
{
score += 5;
scorecounter.text = score.toString();
}

init();


var score4:uint;


clip.buttonMode = true;
target4.addEventListener(MouseEvent.CLICK, on_press4);


function on_press4(event:MouseEvent):void
{
updateScore4();
}

function updateScore4():void
{
score += 5;
scorecounter.text = score.toString();
}

init();


init();

var score5:uint;


clip.buttonMode = true;
target5.addEventListener(MouseEvent.CLICK, on_press5);


function on_press5(event:MouseEvent):void
{
updateScore5();
}

function updateScore5():void
{
score += 5;
scorecounter.text = score.toString();
}

init();

var score6:uint;


clip.buttonMode = true;
target6.addEventListener(MouseEvent.CLICK, on_press6);


function on_press6(event:MouseEvent):void
{
updateScore6();
}

function updateScore6():void
{
score += 5;
scorecounter.text = score.toString();
}

init();

var score7:uint;


clip.buttonMode = true;
target7.addEventListener(MouseEvent.CLICK, on_press7);


function on_press7(event:MouseEvent):void
{
updateScore7();
}

function updateScore7():void
{
score += 5;
scorecounter.text = score.toString();
}

init();

var score8:uint;


clip.buttonMode = true;
target8.addEventListener(MouseEvent.CLICK, on_press8);


function on_press8(event:MouseEvent):void
{
updateScore8();
}

function updateScore8():void
{
score += 5;
scorecounter.text = score.toString();
}

init();
cooldude2000 is offline   Reply With Quote
Old 04-08-2012, 02:31 PM   #2
-:)lauri
Senior Member
 
Join Date: Aug 2008
Location: Helsinki, Finland
Posts: 1,161
Default

Could you please format the code and use [as]-tags.
-:)lauri is offline   Reply With Quote
Old 04-09-2012, 11:52 AM   #3
cooldude2000
Registered User
 
Join Date: Mar 2012
Posts: 36
Default //

Quote:
Originally Posted by -:)lauri View Post
Could you please format the code and use [as]-tags.

Sorry im new....
And i didnt put the whole code in the last post...

ActionScript Code:
{     resume_btn.visible = false;     backpause.visible = false;     menu.visible = false;     main.visible = false; } var array:Array = [ target, target1, target2, target3, target4, target5, target6, target7, target8 ]; var velX:Number = 8; var velY:Number = 8; init(); function init():void { for each (var b:MovieClip in array) { b.addEventListener(MouseEvent.CLICK, ballClick); b.buttonMode = true; b.mouseChildren = false; } resume_btn.addEventListener(MouseEvent.CLICK, resumeClick); stop_btn.addEventListener(MouseEvent.CLICK, pauseClick); stage.addEventListener(Event.ENTER_FRAME, updateBall); } function ballClick(e:MouseEvent):void { var b:MovieClip = e.target as MovieClip; removeChild(b); array.splice(array.indexOf(b), 1); trace(array.length); } function pauseClick(e:MouseEvent):void { stage.removeEventListener(Event.ENTER_FRAME, updateBall); } function resumeClick(e:MouseEvent):void { stage.addEventListener(Event.ENTER_FRAME, updateBall); } function updateBall(event:Event):void { for each (var t:MovieClip in array) { t.x += velX; t.y += velY; if (t.x > stage.stageWidth - t.width / 2 || t.x < 0 + t.width / 2) { velX *= -1; } else if (t.y > stage.stageHeight - t.height / 2 || t.y < 0 + t.height / 2) { velY *= -1; } } } var score:uint; function init10():void {     score = 0;     scorecounter.text = score.toString();     clip.buttonMode = true;     target.addEventListener(MouseEvent.CLICK, on_press); } function on_press(event:MouseEvent):void {     updateScore(); } function updateScore():void {     score += 5;      scorecounter.text = score.toString(); } init(); var score1:uint;     clip.buttonMode = true;     target1.addEventListener(MouseEvent.CLICK, on_press1); function on_press1(event:MouseEvent):void {     updateScore1(); } function updateScore1():void {     score += 5;      scorecounter.text = score.toString(); } init(); var score2:uint;     clip.buttonMode = true;     target2.addEventListener(MouseEvent.CLICK, on_press2); function on_press2(event:MouseEvent):void {     updateScore2(); } function updateScore2():void {     score += 5;      scorecounter.text = score.toString(); } init(); var score3:uint;     clip.buttonMode = true;     target3.addEventListener(MouseEvent.CLICK, on_press3); function on_press3(event:MouseEvent):void {     updateScore3(); } function updateScore3():void {     score += 5;      scorecounter.text = score.toString(); } init(); var score4:uint;     clip.buttonMode = true;     target4.addEventListener(MouseEvent.CLICK, on_press4); function on_press4(event:MouseEvent):void {     updateScore4(); } function updateScore4():void {     score += 5;      scorecounter.text = score.toString(); } init(); init(); var score5:uint;     clip.buttonMode = true;     target5.addEventListener(MouseEvent.CLICK, on_press5); function on_press5(event:MouseEvent):void {     updateScore5(); } function updateScore5():void {     score += 5;      scorecounter.text = score.toString(); } init(); var score6:uint;     clip.buttonMode = true;     target6.addEventListener(MouseEvent.CLICK, on_press6); function on_press6(event:MouseEvent):void {     updateScore6(); } function updateScore6():void {     score += 5;      scorecounter.text = score.toString(); } init(); var score7:uint;     clip.buttonMode = true;     target7.addEventListener(MouseEvent.CLICK, on_press7); function on_press7(event:MouseEvent):void {     updateScore7(); } function updateScore7():void {     score += 5;      scorecounter.text = score.toString(); } init(); var score8:uint;     clip.buttonMode = true;     target8.addEventListener(MouseEvent.CLICK, on_press8); function on_press8(event:MouseEvent):void {     updateScore8(); } function updateScore8():void {     score += 5;      scorecounter.text = score.toString(); } init(); // game timer var count:Number = 30; // amount of time var myTimer:Timer = new Timer(1000,count); // time in ms, count is calling from line above myTimer.addEventListener(TimerEvent.TIMER, countdown); myTimer.start(); function countdown(event:TimerEvent):void {     myText_txt.text = String((count)-myTimer.currentCount); //dynamic txt box shows current count //if and else if statements if (((count)-myTimer.currentCount) == 0) {     gotoAndStop(7); } } stop_btn.addEventListener(MouseEvent.CLICK, pausemenu) function pausemenu (event:MouseEvent):void{     resume_btn.visible = true;     backpause.visible = true;     menu.visible = true;     main.visible = true; } stop_btn.addEventListener(MouseEvent.CLICK, stopMovie); function stopMovie(e:MouseEvent):void {     myTimer.stop(); } main.addEventListener(MouseEvent.MOUSE_UP, mainmenu); function mainmenu (e:MouseEvent){     gotoAndStop(1); } resume_btn.addEventListener(MouseEvent.MOUSE_UP, resumegame); function resumegame (e:MouseEvent):void {     resume_btn.visible = false;     backpause.visible = false;     menu.visible = false;     main.visible = false;     myTimer.start(); }
cooldude2000 is offline   Reply With Quote
Old 04-09-2012, 03:25 PM   #4
-:)lauri
Senior Member
 
Join Date: Aug 2008
Location: Helsinki, Finland
Posts: 1,161
Default

Okay, it resets the app because every time the app returns to frame one the app calls init() function. So if you visit say in frame 7 and then come back to frame 1 the app will be reseted.

To avoid that you could just put the init() function call inside the condition that tests if it have been called yet or not, for example:

ActionScript Code:
if(!target.buttonMode) {     init(); }


* * *

I'd suggest also to use only one init() function call at least in single frame, and also there is no point to make many "click" event listener for single button. Just use single listener and put all tasks inside single eventlistener handler function.

For example change this:
ActionScript Code:
function init():void {     for each (var b:MovieClip in array)     {         b.addEventListener(MouseEvent.CLICK, ballClick);         b.buttonMode = true;         b.mouseChildren = false;     }     resume_btn.addEventListener(MouseEvent.CLICK, resumeClick);     stop_btn.addEventListener(MouseEvent.CLICK, pauseClick);     stage.addEventListener(Event.ENTER_FRAME, updateBall); } function pauseClick(e:MouseEvent):void {     stage.removeEventListener(Event.ENTER_FRAME, updateBall); } //... some code between. Don't remove! stop_btn.addEventListener(MouseEvent.CLICK, pausemenu) function pausemenu (event:MouseEvent):void{     resume_btn.visible = true;     backpause.visible = true;     menu.visible = true;     main.visible = true; } stop_btn.addEventListener(MouseEvent.CLICK, stopMovie); function stopMovie(e:MouseEvent):void {     myTimer.stop(); }

to this:

ActionScript Code:
function init():void {     for each (var b:MovieClip in array)     {         b.addEventListener(MouseEvent.CLICK, ballClick);         b.buttonMode = true;         b.mouseChildren = false;     }     resume_btn.addEventListener(MouseEvent.CLICK, resumeClick);         // you have a click listener for the stop_btn already     stop_btn.addEventListener(MouseEvent.CLICK, pauseClick);     stage.addEventListener(Event.ENTER_FRAME, updateBall); } function pauseClick(event:MouseEvent):void{     stage.removeEventListener(Event.ENTER_FRAME, updateBall);     resume_btn.visible = true;     backpause.visible = true;     menu.visible = true;     main.visible = true;     myTimer.stop(); } //... some code between. Don't remove!

And if you need to do some code snippet many times, it would be better to make single function that handles the task and call function instead of repeating the whole code.

for example change then these:
ActionScript Code:
{     resume_btn.visible = false;     backpause.visible = false;     menu.visible = false;     main.visible = false; } //... some code between. Don't remove! function pauseClick(event:MouseEvent):void{     stage.removeEventListener(Event.ENTER_FRAME, updateBall);     resume_btn.visible = true;     backpause.visible = true;     menu.visible = true;     main.visible = true;     myTimer.stop(); } //... some code between. Don't remove! resume_btn.addEventListener(MouseEvent.MOUSE_UP, resumegame); function resumegame (e:MouseEvent):void {     resume_btn.visible = false;     backpause.visible = false;     menu.visible = false;     main.visible = false;     myTimer.start(); }

to this:

ActionScript Code:
toggleVisible(); //... some code between. Don't remove! function pauseClick(event:MouseEvent):void{     stage.removeEventListener(Event.ENTER_FRAME, updateBall);     toggleVisible();     myTimer.stop(); } //... some code between. Don't remove! resume_btn.addEventListener(MouseEvent.MOUSE_UP, resumegame); function resumegame (e:MouseEvent):void {     toggleVisible();     myTimer.start(); } function toggleVisible():void {     resume_btn.visible = !resume_btn.visible;     backpause.visible = !resume_btn.visible;     menu.visible = !resume_btn.visible;     main.visible = !resume_btn.visible; }

Note that you have also several event listeners for the clic event of the resume_btn button and several handler functions for the click. You should not make many times the same thing.

...and then:

And when all your targets behaves exactly same way when ever clicked, you should use single handler function to to listen the mouse click of them. Add listener for each target in init() function where you already loop them through.

So you should change this:

ActionScript Code:
toggleVisible(); //... some code between. Don't remove! init(); function init():void {     for each (var b:MovieClip in array)     {         b.addEventListener(MouseEvent.CLICK, ballClick);         b.buttonMode = true;         b.mouseChildren = false;     }     resume_btn.addEventListener(MouseEvent.CLICK, resumeClick);     stop_btn.addEventListener(MouseEvent.CLICK, pauseClick);     stage.addEventListener(Event.ENTER_FRAME, updateBall); } //... some code between. Don't remove! var score:uint; function init10():void {     score = 0;     scorecounter.text = score.toString();     clip.buttonMode = true;     target.addEventListener(MouseEvent.CLICK, on_press); } function on_press(event:MouseEvent):void {     updateScore(); } function updateScore():void {     score += 5;      scorecounter.text = score.toString(); } init(); var score1:uint;     clip.buttonMode = true;     target1.addEventListener(MouseEvent.CLICK, on_press1); function on_press1(event:MouseEvent):void {     updateScore1(); } function updateScore1():void {     score += 5;      scorecounter.text = score.toString(); } init(); var score2:uint;     clip.buttonMode = true;     target2.addEventListener(MouseEvent.CLICK, on_press2); function on_press2(event:MouseEvent):void {     updateScore2(); } function updateScore2():void {     score += 5;      scorecounter.text = score.toString(); } init(); var score3:uint;     clip.buttonMode = true;     target3.addEventListener(MouseEvent.CLICK, on_press3); function on_press3(event:MouseEvent):void {     updateScore3(); } function updateScore3():void {     score += 5;      scorecounter.text = score.toString(); } init(); var score4:uint;     clip.buttonMode = true;     target4.addEventListener(MouseEvent.CLICK, on_press4); function on_press4(event:MouseEvent):void {     updateScore4(); } function updateScore4():void {     score += 5;      scorecounter.text = score.toString(); } init(); init(); var score5:uint;     clip.buttonMode = true;     target5.addEventListener(MouseEvent.CLICK, on_press5); function on_press5(event:MouseEvent):void {     updateScore5(); } function updateScore5():void {     score += 5;      scorecounter.text = score.toString(); } init(); var score6:uint;     clip.buttonMode = true;     target6.addEventListener(MouseEvent.CLICK, on_press6); function on_press6(event:MouseEvent):void {     updateScore6(); } function updateScore6():void {     score += 5;      scorecounter.text = score.toString(); } init(); var score7:uint;     clip.buttonMode = true;     target7.addEventListener(MouseEvent.CLICK, on_press7); function on_press7(event:MouseEvent):void {     updateScore7(); } function updateScore7():void {     score += 5;      scorecounter.text = score.toString(); } init(); var score8:uint;     clip.buttonMode = true;     target8.addEventListener(MouseEvent.CLICK, on_press8); function on_press8(event:MouseEvent):void {     updateScore8(); } function updateScore8():void {     score += 5;      scorecounter.text = score.toString(); } init();

to this:

ActionScript Code:
//... some code between. Don't remove! // define all "global" variables on the top of the code var score:uint; function init():void {     for each (var b:MovieClip in array)     {         // you have already a click listener for the target/ball         b.addEventListener(MouseEvent.CLICK, ballClick);         b.buttonMode = true;         b.mouseChildren = false;     }     resume_btn.addEventListener(MouseEvent.CLICK, resumeClick);     stop_btn.addEventListener(MouseEvent.CLICK, pauseClick);     stage.addEventListener(Event.ENTER_FRAME, updateBall);         // make set up for the app in the init() function     toggleVisible();     clip.buttonMode = true; } function ballClick(e:MouseEvent):void {     var b:MovieClip = e.target as MovieClip;     removeChild(b);     array.splice(array.indexOf(b), 1);     trace(array.length);     // update the score every time the ball get clicked     updateScore(); } //... some code between. Don't remove! function updateScore():void {     score += 5;      scorecounter.text = score.toString(); } // and finally check if init() function have been called already or not if(!clip.buttonMode) {     // you should also define all those variables here which you     // don't want to reset automatically when ever the frame 1     // will be called     // and at last call init();     init(); }

...that's for the start.

Last edited by -:)lauri; 04-09-2012 at 03:56 PM.
-:)lauri is offline   Reply With Quote
Old 04-10-2012, 04:16 PM   #5
cooldude2000
Registered User
 
Join Date: Mar 2012
Posts: 36
Default .......

you didnt help me.....
cooldude2000 is offline   Reply With Quote
Old 04-10-2012, 09:11 PM   #6
-:)lauri
Senior Member
 
Join Date: Aug 2008
Location: Helsinki, Finland
Posts: 1,161
Default

In that case you need to be more specific.
-:)lauri is offline   Reply With Quote
Old 04-10-2012, 10:00 PM   #7
rrh
throw a trace() in there
 
Join Date: Dec 2006
Posts: 1,972
Default

Quote:
can you help me continue from where you quit ???
And lauri said
Quote:
Okay, it resets the app because every time the app returns to frame one the app calls init() function. So if you visit say in frame 7 and then come back to frame 1 the app will be reseted.

To avoid that you could just put the init() function call inside the condition that tests if it have been called yet or not, for example:
Code:
if(!target.buttonMode) {
     init();
 }
That seems to me a step in the right direction.

You may need to move other stuff into the init function, like maybe when you set the initial values for the velocities to 8.

And rather than toggleVisible, I might have gone this direction:
Code:
function setVisible(vis:Boolean):void
{
    resume_btn.visible = vis;
    backpause.visible = vis;
    menu.visible = vis;
    main.visible = vis;
}
And then I can call setVisible(true) or setVisible(false);
rrh is online now   Reply With Quote
Old 04-19-2012, 01:14 PM   #8
PasD
pseudoActionscripter
 
Join Date: Apr 2012
Posts: 68
Default

you put your main menu in a movieClip, not frame 1. Put the menu Clip on top of all the others by using stage.setChildIndex(menu,stage.numChildren-1). When you click "Menu" button, the menu MC is visible (.visible = true). Then when you click resume, the .visible is false. That way, everything stays, but in the background.

When you first start the game, the background is blank.
PasD is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 06:10 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.