ActionScript 3 Array Return Runtime Error - arrays

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

Related

Can't figure out how to make xp bar

const bar_fill_start = "<:filledinstart:966733512428384296>"
const bar_fill_end = "<:filledinend:966733512088649759>"
const bar_fill_middle = "<:filledinmiddle:966733512449355796>"
const bar_empty_start = "<:emptystart:966733512457728000>"
const bar_empty_end = "<:emptyend:966733512499691700>"
const bar_empty_middle = "<:emptymiddle:966733512390615041>"
function generateXpBar(xp, level) {
let bar = bar_fill_start + "";
let barLength = 6;
let xpToNextLevel = Levels.xpFor(level);
let xpToNextLevelPercent = xp / xpToNextLevel;
console.log(xpToNextLevelPercent);
for (let i = 0; i < barLength; i++) {
if (i < Math.floor(barLength * xpToNextLevelPercent) - 1) {
bar += bar_fill_middle;
} else {
if(i <= Math.floor(barLength * xpToNextLevelPercent) - 1) {
bar += bar_empty_middle;
}else {
bar += bar_empty_end;
}
}
}
return bar;
}
On audite it looks good. But on kitten it doesn't
what am i doing wrong ?
Can't figure out where is the error I make since it looks good on audite but not on kitten.
if(i <= Math.floor(barLength * xpToNextLevelPercent) - 1) {
to
if(i < barLength - 1) {
after first else

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

Flash CS3 Deleting objects on stage

Ok so I have this minigame inside my main timeline. The minigame creates a bunch of objects dynamically inside an array using addChild(new a0), new a1, new a2 etc... Anyways at the end of the game, there's an option to either restart (resets scores and goes back to starting frame) or finished (goes back a few frames to the "main screen" which is on a different layer and back a few frames. If I choose either options, any of the objects that werent deleted from playing the game (getting a match) are left on the stage even when restarting or going back to the main frame. I've tried various methods of calling removeChild, setting arrays to empty and what not and I can't seem to figure out how to remove them. With the code that I will display here, I get this error:
ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
at flash.display::DisplayObjectContainer/removeChild()
at mousiesDay_fla::MainTimeline/clearGame()[mousiesDay_fla.MainTimeline::frame258:11]
at mousiesDay_fla::MainTimeline/tryAgain()[mousiesDay_fla.MainTimeline::frame258:29]
Here is the code
stop();
scoreWindow.visible = false;
scoreWindowText.visible = false;
finBtn.visible = false;
tryBtn.visible = false;
finBtn.removeEventListener(MouseEvent.CLICK, finished);
tryBtn.removeEventListener(MouseEvent.CLICK, tryAgain);
function clearGame() {
for( var i:int = 0; i < numClips; i++ ) {
removeChild( myClip[i] );
}
myClip.length = 0;
scoreWindow.visible = false;
scoreWindowText.visible = false;
finBtn.visible = false;
tryBtn.visible = false;
finBtn.removeEventListener(MouseEvent.CLICK, finished);
tryBtn.removeEventListener(MouseEvent.CLICK, tryAgain);
}
function finished(evt:MouseEvent) {
clearGame();
gotoAndPlay(256);
}
function tryAgain(evt:MouseEvent) {
clearGame();
gotoAndPlay(257);
}
backBtn.addEventListener(MouseEvent.CLICK, goBack);
function goBack(evt:MouseEvent) {
gotoAndPlay(256);
}
import flash.utils.*;
var myTimer:Timer = new Timer(1000);
myTimer.addEventListener("timer", timedFunction);
myTimer.start();
function timedFunction(eventArgs:TimerEvent) {
var tc:int= 31 - myTimer.currentCount;
pTime.text = tc.toString();
if (myTimer.currentCount > 30) {
for (var k:Number = 0; k < numClips; k++) {
myClip[k].removeEventListener("mouseDown", pieceMove);
myClip[k].removeEventListener("mouseUp", pieceMove);
}
myTimer.reset();
myTimer.stop();
scoreWindow.visible = true;
scoreWindowText.visible = true;
addChild(scoreWindow);
addChild(scoreWindowText);
scoreWindowText.text = "Congratulations. You got " + upgameScore + " / 10. \nClick FINISHED to go back or TRY AGAIN to restart.";
finBtn.visible = true;
finBtn.addEventListener(MouseEvent.CLICK, finished);
addChild(finBtn);
tryBtn.visible = true;
tryBtn.addEventListener(MouseEvent.CLICK, tryAgain);
addChild(tryBtn);
}
}
var mySound:Sound = new correctSound();
upgameScore = 0;
var numClips:Number = 7;
var myClip = new Array(numClips);
myClip[0] = addChild(new a0());
myClip[1] = addChild(new a1());
myClip[2] = addChild(new a2());
myClip[3] = addChild(new a3());
myClip[4] = addChild(new a4());
myClip[5] = addChild(new a5());
myClip[6] = addChild(new a6());
//myClip[7] = addChild(new a7());
//myClip[8] = addChild(new a8());
//myClip[9] = addChild(new a9());
myClip[0].name = "piece0";
myClip[1].name = "piece1";
myClip[2].name = "piece2";
myClip[3].name = "piece3";
myClip[4].name = "piece4";
myClip[5].name = "piece5";
myClip[6].name = "piece6";
//myClip[7].name = "piece7";
//myClip[8].name = "piece8";
//myClip[9].name = "piece9";
var nph = new Array(numClips);
nph[0] = nph0_mc;
nph[1] = nph1_mc;
nph[2] = nph2_mc;
nph[3] = nph3_mc;
nph[4] = nph4_mc;
nph[5] = nph5_mc;
nph[6] = nph6_mc;
//nph[7] = nph7_mc;
//nph[8] = nph8_mc;
//nph[9] = nph9_mc;
var tpg = new Array(numClips);
tpg[0] = tpg0_mc;
tpg[1] = tpg1_mc;
tpg[2] = tpg2_mc;
tpg[3] = tpg3_mc;
tpg[4] = tpg4_mc;
tpg[5] = tpg5_mc;
tpg[6] = tpg6_mc;
//tpg[7] = tpg7_mc;
//tpg[8] = tpg8_mc;
//tpg[9] = tpg9_mc;
var x0 = myClip[0].x = Math.floor(Math.random()*(1+530-20))+20;//Math.random()*400+50;
var y0 = myClip[0].y = Math.floor(Math.random()*(1+380-20))+20;//Math.random()*50+50;
var x1 = myClip[1].x = Math.floor(Math.random()*(1+530-20))+20;//Math.random()*400+50;
var y1 = myClip[1].y = Math.floor(Math.random()*(1+380-20))+20;//Math.random()*50+50;
var x2 = myClip[2].x = Math.floor(Math.random()*(1+530-20))+20;//Math.random()*400+50;
var y2 = myClip[2].y = Math.floor(Math.random()*(1+380-20))+20;//Math.random()*50+50;
var x3 = myClip[3].x = Math.floor(Math.random()*(1+530-20))+20;//Math.random()*400+50;
var y3 = myClip[3].y = Math.floor(Math.random()*(1+380-20))+20;//Math.random()*50+50;
var x4 = myClip[4].x = Math.floor(Math.random()*(1+530-20))+20;//Math.random()*400+50;
var y4 = myClip[4].y = Math.floor(Math.random()*(1+380-20))+20;//Math.random()*50+50;
var x5 = myClip[5].x = Math.floor(Math.random()*(1+530-20))+20;//Math.random()*400+50;
var y5 = myClip[5].y = Math.floor(Math.random()*(1+380-20))+20;//Math.random()*50+50;
var x6 = myClip[6].x = Math.floor(Math.random()*(1+530-20))+20;//Math.random()*400+50;
var y6 = myClip[6].y = Math.floor(Math.random()*(1+380-20))+20;//Math.random()*50+50;
/*var x7 = myClip[7].x = Math.floor(Math.random()*(1+530-20))+20;//Math.random()*400+50;
var y7 = myClip[7].y = Math.floor(Math.random()*(1+380-20))+20;//Math.random()*50+50;
var x8 = myClip[8].x = Math.floor(Math.random()*(1+530-20))+20;//Math.random()*400+50;
var y8 = myClip[8].y = Math.floor(Math.random()*(1+380-20))+20;//Math.random()*50+50;
var x9 = myClip[9].x = Math.floor(Math.random()*(1+530-20))+20;//Math.random()*400+50;
var y9 = myClip[9].y = Math.floor(Math.random()*(1+380-20))+20;//Math.random()*50+50;*/
var j:Number;
for (var k:Number = 0; k < numClips; k++) {
myClip[k].addEventListener("mouseDown", pieceMove);
myClip[k].addEventListener("mouseUp", pieceMove);
}
function pieceMove(evt:Event):void {
if (evt.type == "mouseDown") {
//mySound.play();
evt.target.startDrag();
}
else if (evt.type == "mouseUp") {
//mySound.play();
evt.target.stopDrag();
for (j = 0; j < numClips; j++) {
if (evt.target.name == "piece" + j &&
evt.target.hitTestObject(nph[j]) == true) {
removeChild(myClip[j]);
nph[j].alpha = 0;
tpg[j].alpha = 100;
if (j == 2) {
setChildIndex(tpg[j], 1);
}
upgameScore++;
}
else if (evt.target.name == "piece" + j) {
evt.target.x = Math.floor(Math.random()*(1+530-20))+20;//Math.random()*400+50;
evt.target.y = Math.floor(Math.random()*(1+380-20))+20;//Math.random()*50+50;
}
}
scor.text = upgameScore.toString();
if (upgameScore == 10) {
msgbox.text = "Congratulations !";
for (var k:Number = 0; k < numClips; k++) {
myClip[k].removeEventListener("mouseDown", pieceMove);
myClip[k].removeEventListener("mouseUp", pieceMove);
}
myTimer.reset();
myTimer.stop();
scoreWindow.visible = true;
scoreWindowText.visible = true;
addChild(scoreWindow);
addChild(scoreWindowText);
scoreWindowText.text = "Congratulations. You got " + upgameScore + " / 10. \nClick FINISHED to go back or TRY AGAIN to restart.";
}
}
}
I should mention that if you look near the end of the code where I do the testHitObject and then call removeChild after that, THAT particular delete works and removes the object from the frame.
Solved this one too. I should probably spend a bit more time before I post these.
As it turns out, when objects were being matched they were being removed as per the removeChild() function that was working. What I was doing then was iterating through the array and attempting to remove some objects that were already removed. So what i did was kept an array that matched the objects and when they were removed, changed a flag to 0. Then at the end, iterate through the new array and if there's a 1, remove the child object from the array with the same index. If there's a 0, ignore it. Now it works.

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

AS3 Array object showing up by ordering

I have 3 MC on stage which are all alpha=0
var mcArray:Array = [mc1,mc2,mc3];
for (var j:int = 0; j < mcArray.length; j++)
{
mcArray[j].alpha = 0;
}
I have one button which once I click then it will make 1 of the MC become alpha=1
revealBtn.buttonMode = true;
revealBtn.useHandCursor = false;
revealBtn.addEventListener(MouseEvent.CLICK, revealClick);
function revealClick(event:MouseEvent):void
{
for (var j:int = 0; j < mcArray.length; j++)
{
mcArray[j].alpha = 1;
}
}
But with the script above it will makes all 3 MC become alpha=1.
I know that this can achieve by using below code:
if(mc1.alpha!=1){
mc1.alpha=1
}
if(mc2.alpha!=1){
mc2.alpha=1
}
if(mc3.alpha!=1){
mc3.alpha=1
}
this code will give what I want to achieve but if there is more than 3 MC the lines of script will be longer.
revealBtn.buttonMode = true;
revealBtn.addEventListener(MouseEvent.CLICK, revealClick);
var mcArray:Array = [mc0,mc1,mc2];
function revealClick(event:MouseEvent):void
{
for(var i:uint = 0; i<mcArray.length; i++){
if(this['mc'+i].alpha !== 1){
this['mc'+i].alpha = 1;
break;
}
}
}
No that if statement would still turn all three to alpha 1.
Which of the three do you want to set to alpha = 1 when the click is made?
You could use something like this to set alpha = 1 on the first mc in the array which does NOT have alpha = 1
function revealClick(event:MouseEvent):void {
for (var j:int = 0; j < mcArray.length; j++){
if ( mcArray[j].alpha != 1 ){
mcArray[j].alpha = 1;
return;
}
}
}
Just use a counter.
var cnt : int = -1;
function revealClick(event:MouseEvent):void
{
if(++cnt < mcArray.length) mcArray[cnt].alpha = 1;
}

Resources