Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Working with a countdown timer and Drag & Drop game

Guest
Nov 26, 2010 Nov 26, 2010

Hey guys!

I'm new with flash and I'm having some trouble with my script. The idea is to start the game by pressing the start button, and the countdown starts. The start button will then disappear.

In 60 seconds, the idea is to try to drag all the garbage objects into the respective recycling bins. Only 1 try per object.The object will return to it's original position if it's been placed wrongly, mouseevent for that object is then disabled.

After 60 seconds, the game will end and all mouseevents are disabled. The start button reappears and I can restart the game again.

Issues with my script:

1) During the countdown, when I misplace some of the items, they tend to fly towards the top left of the screen than back to their original positions.

2) I can only press the start button once.

3) How do I reset the game by pressing the button again exactly?

Script:


var count:Number = 60;

var myTimer:Timer = new Timer(1000,count);

myTimer.addEventListener(TimerEvent.TIMER, countdown);

myStartButton.addEventListener(MouseEvent.MOUSE_DOWN, clickStartButton);

function clickStartButton(e:MouseEvent):void{
    myTimer.start();
    myStartButton.visible = false;
   
}

function countdown(e:TimerEvent):void{
    myText_txt.text = String((count)-myTimer.currentCount);
    if(myText_txt.text == "0"){
      myStartButton.visible = true;
      myTimer.reset();
      myStartButton.mouseEnabled = true;
}
   
   

var throwGarbage:Sound = new mySound;
var throwGarbageWrong:Sound = new mySound2;

//6 Garbage Objects//

addEventListener(MouseEvent.MOUSE_DOWN, pickObject);
addEventListener(MouseEvent.MOUSE_UP, releaseObject);

myPlasticBin.mouseEnabled = false;
myPlasticLogo.mouseEnabled = false;
myGlassBin.mouseEnabled = false;
myGlassLogo.mouseEnabled = false;
myPaperBin.mouseEnabled = false;
myPaperLogo.mouseEnabled = false;
myStartButton.mouseEnabled = false;

var orgLocation = new Point;

function pickObject(e:MouseEvent):void
{
        orgLocation = new Point(e.target.x, e.target.y);
        e.target.startDrag();
}

function releaseObject(e:MouseEvent):void
{
    var correct:Boolean = false;
   
    e.target.stopDrag();
   
    if (e.target.dropTarget != null)
    {
        switch (e.target.dropTarget.parent.name)
        {
            case "myPlasticBin":
                if (e.target.name=="myPlasticCup")
                    correct = true;               
                break;
               
            case "myPlasticLogo":
                if (e.target.name=="myPlasticCup")
                    correct = true;               
                break;
           
            case "myGlassBin":
                if (e.target.name=="myBrokenBottle")
                    correct = true;               
                break;
               
            case "myGlassLogo":
                if (e.target.name=="myBrokenBottle")
                    correct = true;               
                break;
       
            case "myPaperBin":
                if (e.target.name=="myCrumpledPaper")
                    correct = true;
               
               
            case "myPaperLogo":
                if (e.target.name=="myCrumpledPaper")
                    correct = true;
           
               
            case "myPaperBin":
                if (e.target.name=="myNewspaper")
                    correct = true;
               
               
            case "myPaperLogo":
                if (e.target.name=="myNewspaper")
                    correct = true;
                break;
               
        }
    }
    if (correct == true)
    {
        e.target.x=e.target.dropTarget.parent.x;
        e.target.y=e.target.dropTarget.parent.y;
        e.target.visible = false;
        throwGarbage.play();
    }
    else
    {
        e.target.x = orgLocation.x;
        e.target.y = orgLocation.y;
        throwGarbageWrong.play();
        e.target.mouseEnabled = false;
    }
       
       
}
}

TOPICS
ActionScript
1.5K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 27, 2010 Nov 27, 2010

1. I don't see where your objects will gradually tend toward anywhere when misplaced... just a direct trip to whatever orgLocation.x;/orgLocation.y are.

2. You may have an issue with trying to do math with String values.  If your Timer is set to count 60 seconds, just assign an event listener (  TimerEvent.TIMER_COMPLETE) to it to trigger your code where you test for the "0" in the textfield instead of testing the 0 in the textfield.

3.  You basically need to reset everything.  What sometimes works well is to have your game in frame 2, and use a trip to frame 1 to restart.  But if your Start button is also going to be your Restart button, then you'll need all the logic to go with resetting everything in its functionality.

Since each object has a fixed target location, you should just assign the target location to each dragged object.  That way, you don't need to whole switch routine.  A simpler if(actualTarget == assignedTarget) approach can be used.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Nov 27, 2010 Nov 27, 2010

Thanks for the reply!

I'm not sure how to get the frames to work properly to reset the game, but I think I'm having more luck using scenes.

I have 2 scenes. I duplicated my original scene to obtain 2 identical scenes. 0scene and 1stscene respectively in order.

On 0scene,

gotoAndStop("1","1stscene");

On 1stscene, just the Countdown and reset part.

stop();

var count:Number = 60;

var myTimer:Timer = new Timer(100,count);

myTimer.addEventListener(TimerEvent.TIMER, countdown);

myStartButton.addEventListener(MouseEvent.MOUSE_DOWN, clickStartButton);

function clickStartButton(e:MouseEvent):void
{
    myTimer.start();
    myStartButton.visible = false;
   
}

function countdown(e:TimerEvent):void
{
    myText_txt.text = String((count)-myTimer.currentCount);
    if(myText_txt.text == "0")
    {
        myStartButton.visible = true;
        myTimer.reset();
        gotoAndStop("1","0scene");
    }

The items do go back to their original position and I'm able to push the start button again to start the countdown timer. Perhaps it would be easier if I just add another button just for reset. 😃

However, after the first cycle, I can't seem to disable the mouseevents for the items before starting the second cycle. I'm still able to drag them.

In regards to the items flying towards the top left hand corner of the screen, it only happens when I misplace the items at the wrong location during the countdown. If there's no countdown, the items move back to their original positions when misplaced and works correctly.

Maybe I should be using frames instead, but I'm not sure how.

Any ideas?

Thanks again!
   

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 27, 2010 Nov 27, 2010

Just treat frames the same way you are treating scenes.  When you compile a file it just joins all the scenes into one timeline anyways.

As far as resetting goes, I can't take the time to go thru the details, but you pretty much need to realize what the states of everything are bewfore you started the first round, and that is what you need to restore things to when you reset to play again.  I would have a reset button separate from the start button, and I would only use it to reset things... leaving the start button to be used again to start the new round.

To use the frames approach I mentioned for a reset, you essentiall have nothing in the first frame, and have you code all in frame 2 (and onward if any).  So if you use gotoAndPlay(1) for your reset button, it essentially heads back to where nothing is and reenters the game as if it was just starting for the first time.

Once you have the functionality you can place portions of the game layout and pieces in frame 1, just nothing coded and on separate layers from the real objects used in the game.  That way, when you do a reset you aren't getting a quick flash of blank space.

If you happen to be adding content dynamically for this game, then using the frame-based reset won't work for removing dynamically added objects.  So you'd need to remove those with code.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Nov 27, 2010 Nov 27, 2010

Thanks! That helped a lot! I implemented a reset button and everything seems to be working fine.

I have one last issue unresolved:

I'm trying to make my timer value stop at the moment all the items have been placed into the correct recycling bins.

Timer Script:

var count:Number = 60;

var myTimer:Timer = new Timer(1000,count);


myTimer.addEventListener(TimerEvent.TIMER, countdown);


function countdown(e:TimerEvent):void
{
    myText_txt.text = String((count)-myTimer.currentCount);
    if(myText_txt.text == "0")
    {
        timeUp.play();
        timeUp.mouseEnabled = false;
        gotoAndStop(3);
       
       
    }
}

I got the script above online so I'm not exactly sure what I should be writing to get it to work.

Using "myTimer.stop()" makes the Timer disappear instead of stop at the value where all the items have been placed correctly.

Thanks so much for all the help so far!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 28, 2010 Nov 28, 2010

It sounds like you don't have an issue with determining when all pieces have been placed properly, so to get the Timer to stop writing to the textfield you can remove its event listener and stop or reset it...

myTimer.reset();  // or stop();

myTimer.removeEventListener(TimerEvent.TIMER, countdown);

 

I don't know what you mean by saying the Timer disappears... it is not a visible object.  A stop() command just stops it where it is.  A reset sets it back to initial conditions.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Nov 28, 2010 Nov 28, 2010

Ah, so sorry. I forgot to mention I'm actually displaying the coundown on screen, so when all the items have been placed in the respective bins correctly, the countdown number doesn't stop at the moment the game ends, but instead disappears totally.

I'm trying to find a command to allow the countdown number to "pause" and display the number which it's at, so to speak, when the game ends. It starts from 60 to 0. So if the game ends with 30 seconds remaining, I want it to display 30 and pause there, till I press the reset button to reset everything.

Thanks!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 29, 2010 Nov 29, 2010
LATEST

I don't see anything in your code that would make the textfield empty out or disappear. Do you change frames such that you go someplace where the textfield isn't.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines