Issue with accessing parts of the array - arrays

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();
}
}

Related

Scrolling through an array

So I have a project in GameMaker, which has a chatbox. The messages for this are stored in an array. I would like to be able to scroll through this array, so I can view earlier chat messages.
This is what I currently have:
Create Event
chatLog[0] = "";
chatIndex = 0;
Step Event
if (chatIndex > 0) {
if (mouse_wheel_down()) {
chatIndex--;
}
}
if (chatIndex < array_length_1d(chatLog) - 1) {
if (mouse_wheel_up()) {
chatIndex++;
}
}
var _maxLines = 5;
for (i = 0; i < _maxLines; i++) {
if (i > (array_length_1d(chatLog) - 1)) { exit; }
var _chatLength = array_length_1d(chatLog) - 1;
draw_text(0, 50 - chatHeight, chatLog[_chatLength - i + chatIndex]);
}
First, for convenience of being able to add messages to front / remove them from the back (once there are too many), let's suppose that the log is a list, item 0 being the newest message,
chatLog = ds_list_create();
chatIndex = 0;
for (var i = 1; i <= 15; i++) {
ds_list_insert(chatLog, 0, "message " + string(i));
}
then, the Step Draw event can use information from the list to clamp scroll offset range and draw items:
var maxLines = 5;
// scrolling:
var dz = (mouse_wheel_up() - mouse_wheel_down()) * 3;
if (dz != 0) {
chatIndex = clamp(chatIndex + dz, 0, ds_list_size(chatLog) - maxLines);
}
// drawing:
var i = chatIndex;
var _x = 40;
var _y = 200;
repeat (maxLines) {
var m = chatLog[|i++];
if (m == undefined) break; // reached the end of the list
draw_text(_x, _y, m);
_y -= string_height(m); // draw the next item above the current one
}
live demo

as3 how to remove/ move position of graphics from reset array

I am trying to reset a scene an move every thing to its original position the reset function resets the array adds the nape bodies back to the stage and attaches the graphics but the original graphics still are on the stage in whatever position they were in when reset was called
private var brickGraphic:MovieClip = new Brick();
private var brick:Body;
private var brickArray:Array;
private function setUp():void
{
brickArray = new Array ;
for (var i:int = 0; i < 10; i++)
{
var brick:Body = new Body(BodyType.DYNAMIC);
var brickShape:Polygon = new Polygon(Polygon.box(10,25));
var brickGraphic = new Brick();
brickGraphic.width = 10;
brickGraphic.height = 25;
addChild(brickGraphic);
brickGraphic.cacheAsBitmap = true;
brick.shapes.add(brickShape);
brick.position.setxy(450, ((ag ) - 30 * (i + 0.5)));
brick.angularVel = 0;
brick.shapes.at(0).material.elasticity = .5;
brick.shapes.at(0).material.density = 150;
brick.cbTypes.add(brickType);
brick.space = space;
brickGraphic.stop();
brick.userData.sprite = brickGraphic;
brick.userData.sprite.x = brick.position.x;
this.brickArray.push(brick);
}
private function reset():void
{
if (contains(brickGraphic)) removeChild(brickGraphic);
space.clear();
setUp();
}
}
this is the final issue i am having on this app and your help would be greatly appreciated
That's because you are not removing them with removeChild.
You need to call removeChild for each brickGraphic object you add to the stage.
Something like :
private function setUp():void
{
brickArray = [];
for (var i:int = 0; i < 10; i++)
{
var brick:Body = new Body(BodyType.DYNAMIC);
var brickShape:Polygon = new Polygon(Polygon.box(10,25));
var brickGraphic = new Brick();
brickGraphic.width = 10;
brickGraphic.height = 25;
addChild(brickGraphic);
brickGraphic.cacheAsBitmap = true;
brick.shapes.add(brickShape);
brick.position.setxy(450, ((ag ) - 30 * (i + 0.5)));
brick.angularVel = 0;
brick.shapes.at(0).material.elasticity = .5;
brick.shapes.at(0).material.density = 150;
brick.cbTypes.add(brickType);
brick.space = space;
brickGraphic.stop();
brick.userData.sprite = brickGraphic;
brick.userData.sprite.x = brick.position.x;
this.brickArray.push(brick);
}
}
private function removeAllBricks():void
{
for(var i:int=0; i<brickArray.length; i++)
{
var dp:DisplayObject = brickArray[i].userData.sprite as DisplayObject;
if(dp && dp.parent)
dp.parent.removeChild(dp);
}
}
private function reset():void
{
removeAllBricks();
space.clear();
setUp();
}

Group an array with same value in actionscript

I have an array:
var exArr:Array = [5,6,10,6,5,11,7,9,12,8,8,13,7,9,14];
I want to array:
var resultArr:Array = [5,6,7,8,9,10,11,12,13,14];
This may use full to you.
var a:Array = [5,6,10,6,5,11,7,9,12,8,8,13,7,9,14];
a.sort();
var i:int = 0;
while(i < a.length) {
while(i < a.length+1 && a[i] == a[i+1]) {
a.splice(i, 1);
}
i++;
}
for other, see here
Try this:
var exArr:Array = [5,6,10,6,5,11,7,9,12,8,8,13,7,9,14];
function group(subject:Array):Array
{
var base:Array = subject.slice().sort(Array.NUMERIC);
var prev:Number = base[0];
for(var i:int = 1; i < base.length; i++)
{
if(base[i] === prev)
{
base.splice(i, 1);
i--;
}
prev = base[i];
}
return base;
}
trace( group(exArr) );

ActionScript 3 Array Return Runtime Error

All right so I have the following code and all it does is put 3 solid-colour squares on the screen and one rainbow-coloured one in the bottom-right corner. When the user presses on any of the solid-coloured squares, that spot get filled with the rainbow-coloured one and in the original location of the rainbow goes that square that was clicked. The code works almost perfectly except for one thing. When the user tries to click on a square that is UNDER the rainbow square, it returns a run-time error.
My Code:
i
mport flash.display.DisplayObject;
import flash.ui.Mouse;
var t1:DisplayObject = new mc_1;
var t2:DisplayObject = new mc_2;
var t3:DisplayObject = new mc_3;
var t4:DisplayObject = new mc_4;
var tile:Array = [[t1,t2],[t3,t4]];
var r:int;
var c:int;
var a:int = 50;
var b:int = 50;
var aa:int = 1;
var bb:int = 1;
function reDraw() {
a = 50;
b = 50;
for (r=0;r<2;r++) {
for (c=0;c<2;c++) {
tile[r][c].x = a;
tile[r][c].y = b;
trace(tile[r][c]);
stage.addChild(tile[r][c]);
tile[r][c].addEventListener(MouseEvent.CLICK, go);
a += 100;
}
a = 50;
b += 100;
}
}
reDraw();
function go(e:MouseEvent):void {
trace(e.target);
//Right:
if (e.target == tile[aa][bb+1]) {
tile[aa][bb] = e.target;
bb += 1;
tile[aa][bb] = t4;
reDraw();
trace("Right");
}
//Left:
else if (e.target == tile[aa][bb-1]) {
tile[aa][bb] = e.target;
bb -= 1;
tile[aa][bb] = t4;
reDraw();
trace("Left");
}
//Up:
else if (e.target == tile[aa-1][bb]) {
tile[aa][bb] = e.target;
aa -= 1;
tile[aa][bb] = t4;
reDraw();
trace("Up");
}
//Down:
else if (e.target == tile[aa+1][bb]) {
tile[aa][bb] = e.target;
aa += 1;
tile[aa][bb] = t4;
reDraw();
trace("Down");
}
else trace("FAILED!");
trace(aa +" " + bb);
}
The error:
TypeError: Error #1010: A term is undefined and has no properties. at
win_fla::MainTimeline/go()
If you take a look at your code you have this:
//Down:
else if (e.target == tile[aa+1][bb]) {
tile[aa][bb] = e.target;
aa += 1;
tile[aa][bb] = t4;
reDraw();
trace("Down");
}
now you can see here that its looking for tile[aa+1] however aa = 1 in the beginning so aa+1 = 2 and tile[2] doesn't exist or is undefined. You'll need to change your logic there to something like:
var tileFound:Boolean = false;
for(var i:int = 0; i < 2; i++){
for(var j:int = 0; j < 2; j++){
if(tile[i][j] == e.target){
tileFound = true;
tile[aa][bb] = e.target;
tile[i][j] = t4;
if(i > aa) trace ("Right");
else if(i < aa) trace ("Left");
if(j > bb) trace ("Bottom");
else if(j < bb) trace ("Top");
aa = i;
bb = j;
reDraw();
tileFound = true;
break;
}
}
if(tileFound) break;
}

AS3: which is faster, a for loop or the forEach() array function?

I'm wondering which is faster in AS3:
array.forEach( function(v:Object, ...args):void
{ ... } );
Or
var l:int = array.length;
for ( var i:int = 0; i < l; i++ ) { ... }
for i :)
var array:Array = [];
for (var i:int=0; i < 100000; i++)
{
array[i] = i;
}
var time:uint = getTimer();
array.forEach( function(v:Object, ...args):void
{ v = 1; } );
trace(getTimer()-time); //trace 85
time = getTimer();
var l:int = array.length;
for ( i = 0; i < l; i++ ) { array[i] = 0; }
trace(getTimer()-time); //trace 3
The above answers do not take into account that you mostly will be performing operations on the array elements in the loop
This code
import flash.utils.getTimer;
var a:Array=[];
var time:int=0;
for(var i:int=0; i<10000; i++) {
a.push(new Date());
}
time=getTimer();
for(var j:int=0; j<a.length; j++) {
var dt:Date=a[j];
dt.valueOf();
}
trace("for: " , getTimer()-time);
time=getTimer();
for each(var xt:Date in a) {
xt.valueOf();
}
trace("for each: " , getTimer()-time);
time=getTimer();
a.forEach(nothing);
trace("a.forEach: " , getTimer()-time);
function nothing(d:Date, ...args):void {
d.valueOf();
}
Returns the following:
for: 3
for each: 2
a.forEach: 13
For 100000 values, the results are starker still
for: 27
for each: 17
a.forEach: 138
Overall winner: the for each loop
for each(var d:myClass in myCollection) {
//myCode
}
For VS Foreach on Array performance (in AS3/Flex)
hope this will help you in understanding the difference between for and for-each loop.
var time:uint;
var vec:Vector.<Number> = new Vector.<Number>;
for (var b:int=0; b < 1000000; b++)
{
vec[b] = 99;
}
///
time = getTimer();
for (var i:int = 0; i < vec.length; i++ )
{
vec[i] = 2;
}
trace('for i: '+(getTimer()-time)+'ms');
///
time = getTimer();
for (var j:int = vec.length - 1; j >= 0; --j)
{
vec[j] = 3;
}
trace('for i rev: '+(getTimer()-time)+'ms');
///
time = getTimer();
for ( var k:int in vec)
{
vec[k] = 4;
}
trace('for: '+(getTimer()-time)+'ms');
///
time = getTimer();
for each(var o:Number in vec)
{
o = 5;
}
trace('for each: '+(getTimer()-time)+'ms');
///
time = getTimer();
vec.forEach( function(v:int, ...args):void
{
v = 6;
}
);
trace('forEach: '+(getTimer()-time)+'ms');
// for i: 81ms
// for i rev: 79ms
// for: 28ms
// for each: 33ms
// forEach: 530ms

Resources