Need the timer to reset the game when it runs out - timer

I need the timer to be able to reset when the game ends. The Timer is set to 1:05 to count down from that value but the timer when it is supposed to reset when it runs out, it does not it just stays at 0:00. Here is the code below:
package {
import flash.display.MovieClip;
import flash.events.TimerEvent;
import flash.utils.Timer;
import flash.events.ThrottleEvent;
public class MainTimer extends MovieClip {
private var currentMin:int;
private var currentSec:int;
private var oneSecondTimer:Timer = new Timer(1000,1);
public var timerHasStopped:Boolean = false;
public function MainTimer() {
// constructor code
trace("the main timer is here");
currentMin = 1;
currentSec = 5;
minBox.text = String(currentMin);
if(currentSec < 10) {
secBox.text = "0" + String(currentSec);
}else{
secBox.text = String(currentSec);
}
oneSecondTimer.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete);
oneSecondTimer.start();
}
private function onTimerComplete(event:TimerEvent):void{
currentSec = currentSec - 1;
if (currentSec < 0){
currentSec = 59;
currentMin = currentMin - 1;
}
if(currentMin < 0) {
currentMin = 0;
currentSec = 0;
}else{
oneSecondTimer.start();
}
minBox.text = String(currentMin);
secBox.text = String(currentSec);
if(currentSec < 10){
secBox.text = "0" + String(currentSec);
}
}
public function resetTimer():void{
//update our display
currentMin = 1;
currentSec = 5;
minBox.text = String(currentMin);
secBox.text = String(currentSec);
//Adjust display for seconds less than 10
if (currentSec < 10){
secBox.text = "0" + String(currentSec);
}//end if
timerHasStopped = false;
oneSecondTimer.start();
}//end function
}
}

I have amended your code. Your code wasn't calling the resetTimer function. Also, I've listen for TIMER instead of TIMER_COMPLETE. When the timer reaches 0, its calls the resetTimer.
package {
import flash.display.MovieClip;
import flash.events.TimerEvent;
import flash.utils.Timer;
//import flash.events.ThrottleEvent;
public class MainTimer extends MovieClip {
private var currentMin:int;
private var currentSec:int;
private var oneSecondTimer:Timer = new Timer(1000);
public var timerHasStopped:Boolean = false;
public function MainTimer() {
// constructor code
trace("the main timer is here");
currentMin = 1;
currentSec = 5;
minBox.text = String(currentMin);
if(currentSec < 10) {
secBox.text = "0" + String(currentSec);
}else{
secBox.text = String(currentSec);
}
oneSecondTimer.addEventListener(TimerEvent.TIMER, onTimerComplete);
oneSecondTimer.start();
}
private function onTimerComplete(event:TimerEvent):void{
trace("TIMER HAS STARTED. COUNTING DOWN.......");
currentSec = currentSec - 1;
if (currentSec < 0){
currentSec = 59;
currentMin = currentMin - 1;
}
if(currentMin < 0) {
currentMin = 0;
currentSec = 0;
resetTimer();
}
minBox.text = String(currentMin);
secBox.text = String(currentSec);
if(currentSec < 10){
secBox.text = "0" + String(currentSec);
}
}
public function resetTimer():void{
oneSecondTimer.removeEventListener(TimerEvent.TIMER, onTimerComplete);
trace("TIMER HAS FINISHED. RESETTING.......");
//update our display
currentMin = 1;
currentSec = 5;
minBox.text = String(currentMin);
secBox.text = String(currentSec);
//Adjust display for seconds less than 10
if (currentSec < 10){
secBox.text = "0" + String(currentSec);
}//end if
timerHasStopped = false;
//if the timer needs to start again, add the following line of code.
oneSecondTimer.addEventListener(TimerEvent.TIMER, onTimerComplete);
}//end function
}
}
Hope this helps.

Related

Error #1009: Cannot access a property or method of a null object for loop()

I have a problem: I have tested the single pong game, and then I found an error when the game live reached 0 and moved to a Game Over screen for a single pong game.
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at BlowfishPong_fla::MainTimeline/loop()
Can you check this code?
Here is my code for the single pong game:
import flash.events.MouseEvent;
import flash.utils.Dictionary;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.media.SoundMixer;
import flash.display.MovieClip;
import flash.text.TextField;
import flash.net.SharedObject;
import flash.events.Event;
stop();
var sound: Sound
var sound_channel: SoundChannel;
var gameMiss: Miss = new Miss();
var gameBounce: BounceWall = new BounceWall();
var gameHit: Hit = new Hit();
var ballSpeedX: int = -3;
var ballSpeedY: int = -2;
var gameScore = 0;
var gameLives = 3;
var plzStop: Boolean = false;
helpContent.visible = false;
reset_btn.addEventListener(MouseEvent.CLICK, scoreReset);
function scoreReset(event: MouseEvent): void {
gameScore = 0;
gameLives = 3;
updateTextFields();
}
pause_btn.addEventListener(MouseEvent.CLICK, goPause);
function goPause(event: MouseEvent): void {
if (plzStop == true) {
plzStop = false;
} else {
plzStop = true;
}
}
home_btn.addEventListener(MouseEvent.CLICK, goHome);
function goHome(event: MouseEvent): void {
plzStop = true;
gotoAndStop("startScreen");
}
help_btn2.addEventListener(MouseEvent.CLICK, goHelp2);
function goHelp2(event: MouseEvent): void {
if (plzStop == true) {
helpContent.visible = false;
plzStop = false;
} else {
helpContent.visible = true;
plzStop = true;
}
}
function init(): void {
score_txt.text = gameScore;
lives_txt.text = gameLives;
stage.addEventListener(Event.ENTER_FRAME, loop);
}
function updateTextFields(): void {
score_txt.text = gameScore;
lives_txt.text = gameLives;
}
function calculateBallAngle(paddleY: Number, ballY: Number): Number {
var ySpeed: Number = 5 * ((ballY - paddleY) / 25);
return ySpeed;
}
function loop(e: Event): void {
if (plzStop == false) {
characterPaddle.y = mouseY;
blowfishPong.x += ballSpeedX;
blowfishPong.y += ballSpeedY;
if (blowfishPong.x <= blowfishPong.width / 2) {
blowfishPong.x = blowfishPong.width / 2;
ballSpeedX *= -1;
gameBounce.play();
}
else if (blowfishPong.x >= stage.stageWidth - blowfishPong.width / 2) {
blowfishPong.x = stage.stageWidth - blowfishPong.width / 2;
ballSpeedX *= -1;
gameMiss.play();
gameLives--;
lives_txt.text = gameLives;
if(gameLives == 0){
stage.removeEventListener(Event.ENTER_FRAME,loop);
gotoAndStop("gameover_Single");
SoundMixer.stopAll();
}
}
if (blowfishPong.y <= blowfishPong.height / 2) {
blowfishPong.y = blowfishPong.height / 2;
ballSpeedY *= -1;
gameBounce.play();
}
else if (blowfishPong.y >= stage.stageHeight - blowfishPong.height / 2) {
blowfishPong.y = stage.stageHeight - blowfishPong.height / 2;
ballSpeedY *= -1;
gameBounce.play();
}
if (characterPaddle.y - characterPaddle.height / 3 < 0) {
characterPaddle.y = characterPaddle.height / 3;
}
else if (characterPaddle.y + characterPaddle.height / 3 > stage.stageHeight) {
characterPaddle.y = stage.stageHeight - characterPaddle.height / 3;
}
if (characterPaddle.hitTestObject(blowfishPong) == true) {
if (ballSpeedX > 0) {
ballSpeedX *= -1;
ballSpeedY = calculateBallAngle(characterPaddle.y, blowfishPong.y);
gameScore++;
updateTextFields();
gameHit.play();
}
}
}
}
init();
Then, for the PlayerSkin function, there are some errors to fix:
character.addEventListener(Event.ENTER_FRAME, playerSkin);
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at BlowfishPong_fla::MainTimeline/goHome3()[BlowfishPong_fla.MainTimeline::frame28:55]
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at BlowfishPong_fla::MainTimeline/playerSkin()[BlowfishPong_fla.MainTimeline::frame28:268]

How to let Stage show the pictures after timer

After I click start button, my program will not show the second scene and thread , until the thread is finish. And the thread will not run correctly(grass didn't increase).
public class test111 extends Application {
private static int grass_i=130;
private static int grass_j=200;
private static int[][] map = new int[grass_i][grass_j];//草地資料grassdata
private static int totalgrass; //grass total number
private static int totalgrassrest; //grass rest number
private static int months=1; //月
private Timer timer;//時間
//GUI
Scene menusc ;
//START後的視窗
BorderPane bpAll = new BorderPane();
Pane playView = new Pane();
Scene mainsc = new Scene(bpAll);
//草
Image littlegrassImg = new Image(getClass().getResourceAsStream("map_littlegrass.png"));
Image midiumgrassImg = new Image(getClass().getResourceAsStream("map_midiumgrass.png"));
Image muchgrassImg = new Image(getClass().getResourceAsStream("map_muchgrass.png"));
Rectangle2D[][] viewportRect = new Rectangle2D[130][200];
private static ImageView[][] mapView = new ImageView[130][200];
//時間
#Override
public void start(Stage primaryStage) throws InterruptedException {
//MENU
Pane menu = new Pane();
menusc = new Scene(menu);
menu.setPrefSize(1000, 800);
Image MenuImg = new Image(getClass().getResourceAsStream("Menu_title.jpg"));
Image StartImg = new Image(getClass().getResourceAsStream("Start.jpg"));
menu.getChildren().add(new ImageView(MenuImg));
Button StartBt = new Button();
StartBt.setPrefSize(450, 150);
StartBt.setGraphic(new ImageView(StartImg));
StartBt.setMaxSize(450, 150);
StartBt.setLayoutX(300);
StartBt.setLayoutY(600);
menu.getChildren().addAll(new ImageView(MenuImg),StartBt);
primaryStage.setMinHeight(800);//固定視窗大小fix the size of Stage
primaryStage.setMinWidth(1000);//固定視窗大小fix the size of Stage
primaryStage.setMaxHeight(800);//固定視窗大小fix the size of Stage
primaryStage.setMaxWidth(1000);//固定視窗大小fix the size of Stage
primaryStage.setScene(menusc);
primaryStage.setTitle("Hypnos' yard");
//START
StartBt.setOnAction(e->{
BorderPane bp2 = bp2();
menusc = new Scene(bp2);
primaryStage.setScene(menusc);
});
primaryStage.show();
}
public BorderPane bp2(){
playView = playView();
bpAll = new BorderPane();
bpAll.setPrefSize(1000, 800);
playView.setPrefSize(1000, 650); //庭院
bpAll.setTop(playView);
Random ran = new Random();
totalgrass = (ran.nextInt(50)*1000)+48000;
totalgrassrest = totalgrass;
grasscreate();
//設定初始草地construct the initial grass
grassGraphicsLoading(); //loading grass pictures
return bpAll;
}
public Pane playView(){
playView = new Pane();
while(months<12){
timer = new Timer();
TimerTask task = new TimerTask(){
public void run(){
month();
}
};//after program executing 0.2s, a month past
timer.schedule(task, 200);
try {
Thread.sleep(200);
}
catch(InterruptedException e6) {
}
timer.cancel();
}
return playView;
}
//月month
protected void month(){
if(months<13){
grassgrow(totalgrass*2);//grass growth
Platform.runLater(new Runnable(){
#Override
public void run(){
grassGraphicsLoading();
}
});
months++;
}
}
//把草地資料帶入圖形loading grass pictures
private void grassGraphicsLoading(){
for (int i = 0; i < grass_i; i++) { // 設定imageView的圖形和位置
for (int j = 0; j < grass_j; j++) {
viewportRect[i][j] = new Rectangle2D(j * 5, i * 5, 5, 5);
if (170 <= j && j <= grass_j - 1) {
mapView[i][j] = new ImageView(muchgrassImg);
} else if (140 <= j && j < 170) {
mapView[i][j] = new ImageView(muchgrassImg);
} else {
if (map[i][j] == 0) {
mapView[i][j] = new ImageView(littlegrassImg);
} else if (map[i][j] == 1 || map[i][j] == 2) {
mapView[i][j] = new ImageView(midiumgrassImg);
} else {
mapView[i][j] = new ImageView(muchgrassImg);
}
}
playView.getChildren().add(mapView[i][j]);
mapView[i][j].setViewport(viewportRect[i][j]);
mapView[i][j].setX(j * 5);
mapView[i][j].setY(i * 5);
}
}
}
public static void main(String[] args) {
launch(args);
}
// 每經過30天(一個月)草地+1grassgrowth
protected void grassgrow(int sum) {
for (int i = 0; i < grass_i; i++) {
for (int j = grass_j - 1; j >= 0; j--) {
if (map[i][j] < 4 && totalgrass <sum) {
map[i][j] += 1;
totalgrass++;
}
if (totalgrass == 104000||totalgrass ==sum) {
break;
}
}
}
}
//建構草地construct grass
protected void grasscreate() {
for (int j = grass_j - 1; j >= 0; j--) {
for (int i = grass_i - 1; i >= 0; i--) {
if (170 <= j && j <= grass_j - 1) {
map[i][j] = 0;
} else if (140 <= j && j < 170) {
map[i][j] = 4;
totalgrassrest -= 4;
} else if (totalgrassrest <= 0) {
map[i][j] = 0;
} else if (4 * (j + 1) * grass_i <= totalgrassrest) {
map[i][j] = 4;
totalgrassrest -= 4;
} else if (totalgrassrest < 4) {
map[i][j] = totalgrassrest;
totalgrassrest = 0;
} else {
map[i][j] = rancreate();
totalgrassrest -= map[i][j];
}
}
}
totalgrass -= totalgrassrest;
totalgrassrest = 0;
}
// 一格(5X5)內草地數量隨機1-4平米z there is 1-4 grass inside a rectangle
private int rancreate() {
Random ran = new Random();
int grass = ran.nextInt(5);
return grass;
}
}`

Submit Best time to the new scene unity3d

It's my timer that counts time. Is it possible when the game gets to the new scene automatically insert the previous scene's best time? what code should i insert in the new scene to expose the best time?
/**
* TIMER (JAVASCRIPT)
* Copyright (c) gameDev7
*
* This is an HH:MM:SS count down/up timer
* Includes functions for pausing timer
*
* TIP: Search where to place your functions
* 1. Press Cntrl/Cmd + F
* 2a. Type UP for count up timer (CAPS)
* 2b. Type DOWN for count down timer (CAPS)
* 3. Check Match whole word only
* 4. Check Match case
* 5. Click Find Next
*/
/// INPUT VARIABLES
var timerStyle:GUIStyle;
var countdown:boolean = false; //switches between countdown and countup
var hours:float = 0f;
var minutes:float = 1f;
var seconds:float = 30f;
var printDebug:boolean = false; //for debugging purposes
/// CALCULATION VARIABLES
private var pauseTimer:boolean = false;
private var timer:float = 0f;
private var hrs:float = 0f;
private var min:float = 0f;
private var sec:float = 0f;
/// DISPLAY VARIABLES
private var strHours:String = "00";
private var strMinutes:String = "00";
private var strSeconds:String = "00";
private var strHrs:String = "00";
private var strMin:String = "00";
private var strSec:String = "00";
private var message:String = "Timing...";
// Use this for initialization
//end start
// Update is called once per frame
function Update () {
if(Input.GetKeyUp("j")) {
if(pauseTimer) {
pauseTimer = false;
} else {
pauseTimer = true;
}//end if
if(printDebug) print("TimerJS - Paused: " + pauseTimer);
}//end if
if(pauseTimer) {
message = "Timer paused";
Time.timeScale = 0;
} else {
//message = "Timer resumed";
Time.timeScale = 1;
}//end if
if(seconds > 59) {
message = "Seconds must be less than 59!";
return;
} else if (minutes > 59) {
message = "Minutes must be less than 59!";
} else {
FindTimer();
}//end if
}//end update
/* TIMER FUNCTIONS */
//Checks which Timer has been initiated
function FindTimer() {
if(!countdown) {
CountUp();
} else
{
CountDown();
}//end if
}//end FindTimer
//Timer starts at 00:00:00 and counts up until reaches Time limit
function CountUp() {
timer += Time.deltaTime;
if(timer >= 1f) {
sec++;
timer = 0f;
}//end if
if(sec >= 60) {
min++;
sec = 0f;
}//end if
if(min >= 60) {
hrs++;
min = 0f;
}//end if
if(sec >= seconds && min >= minutes && hrs >= hours) {
sec = seconds;
min = minutes;
hrs = hours;
message = "Time limit reached!";
if(printDebug) print("TimerJS - Out of time!");
///----- TODO: UP -----\\\
}//end if
}//end countUp
//Timer starts at specified time and counts down until it reaches 00:00:00
function CountDown() {
timer -= Time.deltaTime;
if(timer <= -1f) {
sec--;
timer = 0f;
}//end if
if(hrs <= 0f) {
hrs = 0f;
}//end if
if(min <= 0f) {
hrs--;
min = 59f;
}//end if
if(sec <= 0f) {
min--;
sec = 59f;
}//end if
if(sec <= 0 && min <= 0 && hrs <= 0) {
sec = 0;
min = 0;
hrs = 0;
message = "Time's Up!";
if(printDebug) print("TimerJS - Out of time!");
///----- TODO: DOWN -----\\\
}//end if
}//end countDown
function FormatTimer () {
if(sec < 10) {
strSec = "0" + sec.ToString();
} else {
strSec = sec.ToString();
}//end if
if(min < 10) {
strMin = "0" + min.ToString();
} else {
strMin = min.ToString();
}//end if
if(hrs < 10) {
strHrs = "0" + hrs.ToString();
} else {
strHrs = hrs.ToString();
}//end if
if(seconds < 10) {
strSeconds = "0" + seconds.ToString();
} else {
strSeconds = seconds.ToString();
}//end if
if(minutes < 10) {
strMinutes = "0" + minutes.ToString();
} else {
strMinutes = minutes.ToString();
}//end if
if(hours < 10) {
strHours = "0" + hours.ToString();
} else {
strHours = hours.ToString();
}//end if
}//end formatTimer
/* DISPLAY TIMER */
function OnGUI () {
FormatTimer();
if(!countdown) {
GUI.Label(new Rect(Screen.width/4-155,Screen.height/3-10,200,90), strHrs + ":" + strMin + ":" + strSec + "", timerStyle);
} //end if
}//end onGui
/* GETTERS & SETTERS */
public function GetPaused() { return pauseTimer; }
public function SetPaused(val:boolean) { pauseTimer = val; }
public function GetSec() { return sec; }
public function GetMin() { return min; }
public function GetHrs() { return hrs; }
public function GetMessage() { return message; }
public function SetMessage(val:String) { message = val; }
Save your best time as a static variable in your Timer script (or whatever you have named it): static var bestTime = 01:32:12.
You can then access it globally from any other script (such as one used on a different scene) as: Timer.bestTime.
You could save the time as a static variable which will make it available across scenes. Alternatively you could use DontDestroyOnLoad(gameObject) to preserve the game object that contains that script which means the game object and all the values of the components attached to it will retain their values. Be careful of using the second approach because if you go back to the scene that originally had the object, you will have duplicates. I believe the best solution in this case would be to just use a static variable and use it across all scenes.

Creating Enemy Cone of vision

I am trying to create a cone of vision for my enemy class. Right now it checks to see if the player is within a radius before moving towards them and will move around randomly if they are not. I want to give it vision so the enemy does not always rotate towards the player.
Enemy Class
package
{
import flash.automation.ActionGenerator;
import flash.events.Event;
import flash.geom.Point;
public class Enemy extends GameObject
{
var isDead:Boolean = false;
var speed:Number = 3;
var originalValue:Number = 50;
var changeDirection:Number = originalValue;
var checkDirection:Number = 49;
var numberGen:Number;
//var startTime:int = getTimer();
//var $timer = getTimer();
//var myTimer:int = getTimer();
//var moveTimer:int = timer - $timer;
public function Enemy()
{
super();
target = target_mc;
}
override public function update():void
{
movement();
}
private function movement():void
{
if (changeDirection >= 0)
{
if (changeDirection > checkDirection)
{
numberGen = (Math.random() * 360) / 180 * Math.PI;
}
var velocity:Point = new Point();
var $list:Vector.<GameObject > = getType(Player);
var $currentDistance:Number = Number.MAX_VALUE;
for (var i:int = 0; i < $list.length; i++)
{
var currentPlayer:GameObject = $list[i];
if (MathUtil.isWithinRange(currentPlayer.width,currentPlayer.x,currentPlayer.y,width,x,y))
{
var $delta:Point = new Point ;
$delta.x = currentPlayer.x - x;
$delta.y = currentPlayer.y - y;
if ($currentDistance > $delta.length)
{
$currentDistance = $delta.length;
velocity = $delta;
velocity.normalize(speed);
}
}
}
if(velocity.length == 0)
{
velocity = Point.polar(2, numberGen);
}
changeDirection--;
if (changeDirection == 0)
{
changeDirection = originalValue;
}
}
velocity.x = Math.floor(velocity.x * 10) * .1;
velocity.y = Math.floor(velocity.y * 10) * .1;
moveBy([Wall, Door], velocity.x, velocity.y, collides_mc);
var $collides:GameObject = collision([Player]);
if ($collides != null)
{
destroy();
}
if ($collides == null)
{
$collides = collision([Player],this);
if ($collides != null)
{
$collides.destroy();
}
}
rotation = (Math.atan2(velocity.y, velocity.x) * 180 / Math.PI);
collides_mc.rotation = -rotation;
trace($collides);
}
override public function onCollideX($collision:GameObject):void
{
}
override public function onCollideY($collision:GameObject):void
{
}
}
}
GameObject Class, which contains all the functions and Enemy extends it
package
{
import flash.display.DisplayObject;
import flash.events.Event;
import flash.ui.Keyboard;
import flash.events.KeyboardEvent;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.sensors.Accelerometer;
import flashx.textLayout.elements.ListElement;
public class GameObject extends MovieClip
{
static public var list:Vector.<GameObject> = new Vector.<GameObject>;
protected var hitBox:Sprite;
public var target:MovieClip;
public function GameObject()
{
target=this;
list.push(this);
}
public function update():void
{
}
public function collision(typesColliding:Array, $target:DisplayObject = null):GameObject
{
if($target == null)
$target = target;
for (var i:int = list.length - 1; i >= 0; i--)
{
var item:GameObject = list[i], found:Boolean = false;
for (var f:int = typesColliding.length - 1; f >= 0; f--)
{
if (item is typesColliding[f])
{
found = true;
break;
}
}
if (found && $target.hitTestObject(item.target) && this != item)
{
return item;
}
}
return null;
}
public function moveBy(typesColliding:Array, $x:Number = 0, $y:Number = 0, $target:DisplayObject = null):void
{
var $collision:GameObject;
x += $x;
if (($collision = collision(typesColliding, $target)) != null)
{
x -= $x;
onCollideX($collision);
}
y += $y;
if (($collision = collision(typesColliding, $target)) != null)
{
y -= $y;
onCollideY($collision);
}
}
public function onCollideX($collision:GameObject):void
{
}
public function onCollideY($collision:GameObject):void
{
}
public function getType($class:Class):Vector.<GameObject>
{
var $list:Vector.<GameObject> = new Vector.<GameObject>;
for (var i:int = 0; i < list.length; i++)
{
if (list[i] is $class)
{
$list.push(list[i]);
}
}
return $list;
}
public function destroy():void
{
var indexOf:int = list.indexOf(this);
if (indexOf > -1)
list.splice(indexOf, 1);
if (parent)
parent.removeChild(this);
trace("removing item: "+this+" list: " + list.length + ": " + list);
}
}
}
Any help would be appreciated, thank you.
Changing radius of vision to the cone of vision simply requires computing (after checking the distance) the angle between the direction enemy is facing and the player. If this angle is smaller then some T, then it is in the cone of vision of angle 2T (due to symmetry).
This obviously ignores any walls/obstacles that should limit vision, but it should give you the general idea.

Issue with accessing parts of the array

I am trying to simply generate parts of an array with certain buttons. the code is not really working properly.
The code is as follows:
import flash.events.MouseEvent;
var clock_01: Clock_one = new Clock_one ();
var clock_02: Clock_two = new Clock_two ();
var clock_03: Clock_three = new Clock_three ();
var clock_04: Clock_four = new Clock_four ();
var socket_one:socket;
var clock_x_position = 100;
var clock_y_position = 100;
var clock_Array: Array = new Array ();
clock_Array.push( clock_01,clock_02,clock_03,clock_04);
var clock_counter = 2;
var v = 0;
var c = 0;
clock_display();
function clock_display()
{
for (v; v < clock_counter; v++)
{
addChild(clock_Array[v]);
clock_Array[v].x = clock_x_position;
clock_Array[v].y = clock_y_position;
clock_y_position += 300;
trace( clock_Array [v]);
c = 0;
trace(v);
}
}
go_previous.addEventListener(MouseEvent.CLICK, go_back);
go_next.addEventListener(MouseEvent.CLICK, go_forward);
function go_back(l:MouseEvent)
{
v -= 2;
trace("The v after subtraction of 2" + v);
trace("Going Previous Function Starts ----------------");
for (v; v < clock_counter; v++)
{
removeChild(clock_Array[v]);
trace("The v after child removal" + v);
c++;
if (c == 2)
{
v -= 4;
trace("The v after subtraction in previous function is " + v);
clock_y_position = 100;
clock_counter -= 2;
trace("The clock counter in previous function is" + clock_counter);
clock_display();
}
}
}
function go_forward(l:MouseEvent)
{
v -= 2;
trace("Going Forwarf Function");
for (v; v < clock_counter; v++)
{
removeChild(clock_Array[v]);
trace("The v after subtraction in forward function is " + v);
trace("it atleast goes here");
c++;
if (c == 2)
{
v += 1;
clock_y_position = 100;
clock_counter += 2;
trace("The clock counter is" + clock_counter);
trace("The V is " +v);
clock_display();
}
}
}
Under the go_back function the v is not really getting subtracted by 2 as it is needed to. That's what it shows in the trace anyway. Can somebody please help me out with it?
Try the code below and do let me know if it works.
Pay attention to variable clock_Array_current_position.
It keeps the pointer to array element where your code is at.
import flash.events.MouseEvent;
var clock_01: Clock_one = new Clock_one ();
var clock_02: Clock_two = new Clock_two ();
var clock_03: Clock_three = new Clock_three ();
var clock_04: Clock_four = new Clock_four ();
var socket_one:socket;
var clock_x_position = 100;
var clock_y_position = 100;
var clock_Array: Array = new Array ();
clock_Array.push(clock_01,clock_02,clock_03,clock_04);
var clock_counter = 2;
var clock_Array_current_position = 0;
clock_display();
function clock_display()
{
var i = 0;
for (i; i < clock_counter; i++)
{
addChild(clock_Array[clock_Array_current_position + i]);
clock_Array[clock_Array_current_position+i].x = clock_x_position;
clock_Array[clock_Array_current_position+i].y = clock_y_position;
clock_y_position += 300;
}
}
function clock_remove()
{
var i = 0;
for (i; i < clock_counter; i++)
{
removeChild(clock_Array[clock_Array_current_position+i]);
}
}
go_previous.addEventListener(MouseEvent.CLICK, go_back);
go_next.addEventListener(MouseEvent.CLICK, go_forward);
function go_back(l:MouseEvent)
{
if(clock_Array_current_position > 0)
{
clock_remove();
clock_Array_current_position -= clock_counter;
clock_y_position = 100;
clock_display();
}
}
function go_forward(l:MouseEvent)
{
if(clock_Array_current_position < clock_Array.length-clock_counter)
{
clock_remove();
clock_Array_current_position += clock_counter;
clock_y_position = 100;
clock_display();
}
}

Resources