0

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?

2
  • Can you give a better explanation of what your code is supposed to do? Commented Jun 9, 2013 at 23:43
  • Yep, I have a list of four clocks that needs to be displayed. And 2 of them needs to be displayed at a time. so I put them all in an array. At very beginning of the program i need 2 generated. than I have 2 buttons one for showing the next 2 clocks and one for showing the previous 2 clocks. and that's pretty much it. in simple sentence just generating objects from an array. The main problem i am having is keeping track of where in the array exactly the program is at. Commented Jun 9, 2013 at 23:50

1 Answer 1

1

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();
    }
} 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.