从数组中提取奇数个对象

时间:2013-06-15 23:10:30

标签: actionscript-3

我正试图从阵列中一次获得2个对象。但很快我将使用奇数个长度和拼接项目。

到目前为止,这在数组中使用偶数数字是完美的,但我不知道如何使它与奇数一起工作。我认为它可能工作的方式是要求它检查下一个出现的对象,如果它小于2,则将计数器更改为1.但我甚至不确定如何将其放在代码中。我到目前为止发布了我的代码

import flash.events.MouseEvent;
import flash.net.Socket;


var socket_Array_current_position = 0;
var socket_counter = 2;
var socket_Array: Array = new Array ();

socket_Array.push(socket_one, socket_two,socket_three, socket_four, socket_five, socket_six);



go_next_left.addEventListener(MouseEvent.CLICK, go_left);
go_next_right.addEventListener(MouseEvent.CLICK, go_right);

function go_left(going_left:MouseEvent)
{

    if (socket_Array_current_position > 0)
    {
        socket_remove();
        socket_Array_current_position -=  socket_counter;
        socket_x_position = 125;
        socket_display();
    }

}

function go_right(going_right:MouseEvent)
{
    if (socket_Array_current_position < socket_Array.length-socket_counter)
    {
    socket_remove();
    socket_Array_current_position +=  socket_counter;
    socket_x_position = 125;
    socket_display();
    }
}



socket_display();

function socket_display()
{
    var s = 0;
    for (s; s < socket_counter; s++)
    {
        addChild(socket_Array[socket_Array_current_position + s]);
        socket_Array[socket_Array_current_position + s].x = socket_x_position;
        socket_Array[socket_Array_current_position + s].y = socket_y_position;
        //socket_Array[s].addEventListener(MouseEvent.CLICK, picked);
        socket_x_position = socket_x_position + 275;
    }
}


function socket_remove()
{
    var s = 0;
    for (s; s < socket_counter; s++)
    {

        removeChild(socket_Array[socket_Array_current_position+s]);

    }
}

1 个答案:

答案 0 :(得分:0)

我想您希望一次从数组中显示X对象(在本例中为两个)。无论长度如何。我正在使用Math lib。考虑到我没有使用sdk或Flash尝试下面的代码。

const X_START_POS:int = 125;
const COLUMN_WIDTH:int = 275;
const QTY_SCREEN:int = 2;  
var socket_Array:Array = new Array(); 
var socket_Array_pos:int = 0; 
var socket_Array_target:int = 0; // target is always right 
var socket_Array_on_screen:Array = new Array();

// socket_Array.length must be >= QTY_SCREEN, always.
socket_Array.push(socket_one, socket_two, socket_three, socket_four, socket_five, socket_six); 

go_next_left.addEventListener(MouseEvent.CLICK, go_left);
go_next_right.addEventListener(MouseEvent.CLICK, go_right);

socket_display();

function go_left(going_left:MouseEvent) {
    socket_Array_target = Math.max(socket_Array_pos - QTY_SCREEN, 0);
    socket_display();
}

function go_right(going_right:MouseEvent) {
    socket_Array_target = Math.min(socket_Array_pos + QTY_SCREEN, socket_Array.length - QTY_SCREEN);
    socket_display();
}

function socket_display() {
    socket_remove();
    socket_x_position = X_START_POS;
    var limit:int = socket_Array_target + QTY_SCREEN;
    for (var i = socket_Array_target; i < limit; i++) {
        show_socket(socket_Array[i]);
        socket_x_position += COLUMN_WIDTH; 
    }
    socket_Array_pos = socket_Array_target;
}

function show_socket(asocket:DisplayObject) {
   addChild(asocket);
   asocket.x = socket_x_position;
   asocket.y = socket_y_position;
   socket_Array_on_screen.push(asocket);  // remember me
}

function socket_remove() {
    var qty:int = socket_Array_on_screen.length;
    for (var s = 0; s < qty; s++) {
        removeChild(socket_Array_on_screen.pop());
    }
}