Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 01-10-2011, 03:10 PM   #1
Eagle12
Registered User
 
Join Date: Jan 2011
Posts: 2
Default [AS3] score remove.child...

Hi, i am new to the forum.

I am creating a game in which a timer controlled movieclip (circle) appears on the stage for 60 seconds at a time and then removes itself, appearing again in another random possition on the stage. I also have a main character movieclip (mario) which i have created and in the code have specified if mario hits the circle, i wish for the circle to be removed, 1 point to be added to the score and then the circle added once again in a new random place on the stage.

I seem to be having difficulties with this as whenever i test my game i have a compiler error of: function value used where type void was expected. But when i play the game everything is working fine until the mario character hits the circle. This results in the circle disapearing, the score adding loads of numbers and the circle not reapearing again on the stage. however the timer is still active as the timer trace still appears in the flash output every 60 seconds.

can anyone help?!?


ActionScript Code:
import flash.utils.Timer; import flash.events.TimerEvent; var circle:coin = new coin(); coin.x=Math.random()*stage.stageWidth; coin.y=Math.random()*stage.stageHeight; addChild(coin); var mytimer:Timer=new Timer(60000,1); mytimer.addEventListener(TimerEvent.TIMER,myrandom); mytimer.start(); function myrandom(evt:TimerEvent):void{ coin.x=Math.random()*stage.stageWidth; coin.y=Math.random()*stage.stageHeight; var mytrace = (Math.random() * 60000); trace(mytrace); var secondarytimer:Timer= new Timer(mytrace,1); mytrace.addEventListener(TimerEvent.TIMER,myrandom); secondarytimer.start(); var myscore=0; addEventListener(Event.ENTER_FRAME,newscore); function newscore(event:Event):void { if(mario.hitTestObject(circle)) { myscore+=1; dynamicscore.text=myscore; removeChild(circle); } } addEventListener(Event.ENTER_FRAME,mycheck); function mycheck(event:Event):void { if (mario.stage) { trace("working fine"); } else { myrandom; } }

Last edited by Eagle12; 01-10-2011 at 03:41 PM.
Eagle12 is offline   Reply With Quote
Old 01-10-2011, 03:31 PM   #2
automator
Senior Member
 
Join Date: Jul 2008
Posts: 138
Default

There seems to be alot of typos in the code. I think as a first step, it's a good idea to carefully go through each line to examine for and correct the typos.

For example, line 9 should be:

var mytimer:Timer = new Timer();

but instead you placed mew Timer()

Line 19 has a typo, too. It says TmerEvent instead of TimerEvent
automator is offline   Reply With Quote
Old 01-10-2011, 03:40 PM   #3
Eagle12
Registered User
 
Join Date: Jan 2011
Posts: 2
Default

ah sorry about that. i had to type up the code manually as the computer flash is on doesnt have internet access right now. if anyone has any idea why im having these problems with the code, im open to all suggestions thanks.
Eagle12 is offline   Reply With Quote
Old 01-11-2011, 02:50 PM   #4
automator
Senior Member
 
Join Date: Jul 2008
Posts: 138
Default

So your edited code was still quite a mess. You missed a curly brace so the code didn't work (you forgot to close the myrandom function). You should be more meticulous about these things when you ask other people for help.

So here's the code with the changes and some comments to explain. I tested it with my own assets and it worked fine.
ActionScript Code:
// You named the instance circle, so you should // be referring to circle throughout your code. // You were originally referring to coin. coin // is the name you gave the class. You should // target the instance name, not the class. var circle:coin = new coin(); var myScore = 0; // This creates a timer that counts every 60 seconds. // No need for the second parameter. You originally // gave it a value of 1. That means that the timer // will only count once. You want it to count // indefinitely instead. So you shouldn't put the // second parameter. var mytimer:Timer = new Timer(60000); // Your original line was: // var mytimer:Timer = new Timer(60000, 1); // No need for the 1. So because it was removed, // you no longer need to add that second timer // inside the TimerEvent.TIMER listener, since this  // one will just keep counting. circle.x = Math.random() * stage.stageWidth; circle.y = Math.random() * stage.stageHeight; addChild(circle); this.addEventListener(Event.ENTER_FRAME,newscore); mytimer.addEventListener(TimerEvent.TIMER,myrandom); mytimer.start(); function myrandom(evt:TimerEvent):void {     // Reset the circles position every 60 seconds     circle.x = Math.random() * stage.stageWidth;     circle.y = Math.random() * stage.stageHeight; } function newscore(event:Event):void {     if (mario.hitTestObject(circle))     {         // You had a typo for this myScore variable         // You declared myScore at the top, but         // you were targeting myscore. Variable         // names are case-sensitive.         myScore +=  1;                 // add the toString() method so that         // the number can be displayed in the         // text field         dynamicscore.text = myScore.toString();                 // When mario hits the circle...         // 1. stop the timer         mytimer.stop();                 // 2. change the circle's position         circle.x = Math.random() * stage.stageWidth;         circle.y = Math.random() * stage.stageHeight;                 // 3. start the timer again         mytimer.start();     } }
automator is offline   Reply With Quote
Old 01-12-2011, 05:14 PM   #5
CyanBlue
Super Moderator
 
CyanBlue's Avatar
 
Join Date: Jan 2002
Location: Centreville, VA
Posts: 26,666
Default

[MODERATOR]
Please do not delete the thread when somebody else has helped you... This is the place where everybody learn from others...
[/MODERATOR]
__________________
CyanBlue / Jason Je / Macromedia Certified Flash Developer & Designer
http://CyanBlue.FlashVacuum.com
http://www.FlashVacuum.com
http://tutorials.FlashVacuum.com

Do NOT PM, Email or Call me... Your question belongs right in this forum...
CyanBlue 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 01:10 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, 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.