Skip to main content
Participating Frequently
April 3, 2014
Question

Making a simple ticking clock in AS3 with .as files

  • April 3, 2014
  • 2 replies
  • 731 views

I'm making a simple clock in AS3 as seen on youtube (Doug Winnie) but I want to put the code into separate .as files. This is what I have so far but I keep getting the error 1046: Type was not found or was not a compile time constant: secondHand. But it's saying that the location of the error is Line 3 of Clock, but secondHand isn't even mentioned here?


package  {

import flash.display.MovieClip;

public class Clock extends MovieClip {

import flash.events.TimerEvent;

import flash.utils.Timer;

import flash.events.MouseEvent;

var clockTimer:Timer = new Timer(1000, 60);

    public function Clock(e:TimerEvent):void

    {

        clockTimer.addEventListener(TimerEvent.TIMER_COMPLETE, endTimer);

        }

        function endTimer(e: TimerEvent): void

        {

        trace("Finished");

        }

    }

    }

// startButton and stopBtn are instances of GameButton.

package  {

import flash.display.MovieClip;

import flash.events.MouseEvent;

public class GameButton extends MovieClip {

    public var startButton : GameButton;

    public var stopBtn : GameButton;

    public function GameButton():void

    {

    createListener();

    startButton.buttonLabel.text = "Start";

    stopBtn.buttonLabel.text = "Stop";

    }

    public function createListener():void{

        startButton.addEventListener(MouseEvent.CLICK, startTimer);

        stopBtn.addEventListener(MouseEvent.CLICK, stopTimer);

    }

    function startTimer(e:MouseEvent):void

        {

            clockTimer.start();

            trace ("Timer started.");

            startButton.visible = false;

        }

        function stopTimer(e:MouseEvent):void

        {

            clockTimer.stop();

            trace("Timer stopped.");

            startButton.visible = true;

        }

    }

}

//

package  {

import flash.display.MovieClip;

import flash.events.TimerEvent;

public class secondHand extends MovieClip {

    public function secondHand(e: TimerEvent):void {

        // constructor code

        clockTimer.addEventListener(TimerEvent.TIMER, moveHand);

    }

    public function moveHand(e:TimerEvent):String

    {

        secondHand.rotation = secondHand.rotation + 6;

        trace("timer");

        }

    }

}

This topic has been closed for replies.

2 replies

Amy Blankenship
Legend
April 4, 2014

The problem is that you've named your Class secondHand. Inside it is (apparently) an instance, also named secondHand. The Player has no idea which one you mean. This is one of the reasons for the convention that Class names should be camel case starting with a capital letter and instance names should be camel case starting with a lower case letter. Rename your secondHand Class to SecondHand and the error should go away.

Also note that there doesn't seem to be much point in breaking out the Classes the way you did, because the majority of the logic seems to be in GameButton. GameButton should only dispatch a start and stop event and manage its own state. The Clock should not be inside it.

kglad
Community Expert
Community Expert
April 3, 2014

copy and paste the error message.

Participating Frequently
April 3, 2014
C:\Users\Claire\Desktop\CLOCK\secondHand.as, Line 81018: Duplicate class definition: secondHand.
C:\Users\Claire\Desktop\CLOCK\Clock.as, Line 15000: The class 'Clock' must subclass 'flash.display.MovieClip' since it is linked to a library symbol of that type.
C:\Users\Claire\Desktop\CLOCK\GameButton.as, Line 15000: The class 'GameButton' must subclass 'flash.display.MovieClip' since it is linked to a library symbol of that type.
C:\Users\Claire\Desktop\CLOCK\secondHand.as, Line 15000: The class 'secondHand' must subclass 'flash.display.MovieClip' since it is linked to a library symbol of that type.
kglad
Community Expert
Community Expert
April 3, 2014

you're using a secondHand class different from the one you posted here.