Creating variable names - actionscript-3

I'm having trouble with the following function:
private function whichLevelToLoad():void{
if(levelToLoad == "nothing"){
currentLevel = null;
}
var thisObj:Object = new Object();
if(levelBtnArray!=null){
for(var j:int=levelBtnArray.length-1;j>=0;j--) {
if(levelToLoad == String("level " + (j+1))){
thisObj["level"+(j+1)] = new ["Level"+(j+1)]();--------------------------->The Problem
thisObj["level" + (j+1)].x = 0;
thisObj["level" + (j+1)].y = 0;
addChildAt(thisObj["level" + (j+1)], 0);
currentLevel = thisObj["level" + (j+1)];
}
}
}
}
I'm trying to instatiate 75 objects by using a loop. The line would look like this,"thisObj.level1 = new Level1(); with the numbers going from 1-75. Is this possible? How
do I do it?

Try
if(levelBtnArray!=null){
var levelClass:Class;
for(var j:int=levelBtnArray.length-1;j>=0;j--) {
if(levelToLoad == String("level " + (j+1))){
levelClass = getDefinitionByName( "Level"+(j+1) ) as Class;
thisObj["level"+(j+1)] = new levelClass();
thisObj["level" + (j+1)].x = 0;
thisObj["level" + (j+1)].y = 0;
addChildAt(thisObj["level" + (j+1)], 0);
currentLevel = thisObj["level" + (j+1)];
}
}
}

Related

what and where is the issue in Base64bitEncoder.decodeToByteArray

Now, I do Flash Actionscript 3.0 development, I have a Base64bitEncoder class with the encodeByteArray(...) and decodeToByteArray(...) function.
but when I write the below testing code, but the byteArray.length at before Encoding and after decoding are different, could you help to see see where I wrong?
Test Sample Code as below:
var _loc_2:XML = new XML("<formXML>aaaaa</formXML>");;
var _loc_3:ByteArray;
_loc_3 = new ByteArray();
//_loc_3.endian = Endian.LITTLE_ENDIAN;
_loc_3.writeUTFBytes(_loc_2);
trace("Before Encoding Length:" + _loc_3.length);
var baseString:String = Base64bitEncoder.encodeByteArray(_loc_3);
trace(baseString);
var strArray:ByteArray = Base64bitEncoder.decodeToByteArray(baseString);
trace("After Decoding length:" + strArray.length);
The Output log as below:
Before Encoding Length:5
GFhAGFhA
After Decoding length:**6**
Why the after is 6, where I do wrong?
Base64bitEncoder source code as below:
package
{
import flash.utils.*;
public class Base64bitEncoder extends Object
{
private static const BASE64_CHARS:String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
private static const BASE64_URL_CHARS:String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_!";
public function Base64bitEncoder()
{
return;
}// end function
public static function encode(param1:String, param2:Boolean = false) : String
{
var _loc_3:ByteArray;
_loc_3 = new ByteArray();
_loc_3.writeUTFBytes(param1);
return encodeByteArray(_loc_3, param2);
}// end function
public static function decodeToByteArray(param1:String, param2:Boolean = false) : ByteArray
{
var _loc_3:ByteArray;
var _loc_4:Array;
var _loc_5:Array;
var _loc_6:uint;
var _loc_7:uint;
var _loc_8:uint;
_loc_3 = new ByteArray();
_loc_4 = new Array(4);
_loc_5 = new Array(3);
_loc_6 = 0;
while (_loc_6 < param1.length)
{
// label
_loc_7 = 0;
while (_loc_7++ < 4 && _loc_6 + _loc_7 < param1.length)
{
// label
_loc_4[_loc_7] = param2 == true ? (BASE64_URL_CHARS.indexOf(param1.charAt(_loc_6 + _loc_7))) : (BASE64_CHARS.indexOf(param1.charAt(_loc_6 + _loc_7)));
}// end while
_loc_5[0] = (_loc_4[0] << 2) + ((_loc_4[1] & 48) >> 4);
_loc_5[1] = ((_loc_4[1] & 15) << 4) + ((_loc_4[2] & 60) >> 2);
_loc_5[2] = ((_loc_4[2] & 3) << 6) + _loc_4[3];
_loc_8 = 0;
while (_loc_8++ < _loc_5.length)
{
// label
if (_loc_4[_loc_8 + 1] == 64)
{
break;
}// end if
_loc_3.writeByte(_loc_5[_loc_8]);
}// end while
_loc_6 = _loc_6 + 4;
}// end while
_loc_3.position = 0;
return _loc_3;
}// end function
public static function encodeByteArray(param1:ByteArray, param2:Boolean = false) : String
{
var _loc_3:String;
var _loc_4:Array;
var _loc_5:Array;
var _loc_6:uint;
var _loc_7:uint;
var _loc_8:uint;
_loc_3 = "";
_loc_5 = new Array(4);
param1.position = 0;
while (param1.bytesAvailable > 0)
{
// label
_loc_4 = [];
_loc_6 = 0;
while (_loc_6++ < 3 && param1.bytesAvailable > 0)
{
// label
_loc_4[_loc_6] = param1.readUnsignedByte();
}// end while
_loc_5[0] = (_loc_4[0] & 252) >> 2;
_loc_5[1] = (_loc_4[0] & 3) << 4 | _loc_4[1] >> 4;
_loc_5[2] = (_loc_4[1] & 15) << 2 | _loc_4[2] >> 6;
_loc_5[3] = _loc_4[2] & 63;
_loc_7 = _loc_4.length;
while (_loc_7++ < 3)
{
// label
_loc_5[_loc_7 + 1] = 64;
}// end while
_loc_8 = 0;
while (_loc_8++ < _loc_5.length)
{
// label
_loc_3 = _loc_3 + (param2 == true ? (BASE64_URL_CHARS.charAt(_loc_5[_loc_8])) : (BASE64_CHARS.charAt(_loc_5[_loc_8])));
}// end while
}// end while
return _loc_3;
}// end function
public static function decode(param1:String, param2:Boolean = false) : String
{
var _loc_3:ByteArray;
_loc_3 = decodeToByteArray(param1, param2);
return _loc_3.readUTFBytes(_loc_3.length);
}// end function
}
}
#akmozo, today, I spend some time to study the base64 theory, I fix the issue, thanks.
update code:
public static function encodeByteArray(param1:ByteArray, param2:Boolean = false) : String
{
var _loc_3:String;
var _loc_4:Array;
var _loc_5:Array;
var _loc_6:uint;
var _loc_7:uint;
var _loc_8:uint;
_loc_3 = "";
_loc_5 = new Array(4);
param1.position = 0;
while (param1.bytesAvailable > 0)
{
// label
_loc_4 = [];
_loc_6 = 0;
while (_loc_6 < 3 && param1.bytesAvailable > 0)
{
// label
_loc_4[_loc_6] = param1.readUnsignedByte();
_loc_6 = _loc_6 + 1;
}// end while
_loc_5[0] = (_loc_4[0] & 252) >> 2;
_loc_5[1] = (_loc_4[0] & 3) << 4 | _loc_4[1] >> 4;
_loc_5[2] = (_loc_4[1] & 15) << 2 | _loc_4[2] >> 6;
_loc_5[3] = _loc_4[2] & 63;
/*
trace("--------------------------------");
trace("0:" + _loc_4[0]);
trace("1:" + _loc_4[1]);
trace("3:" + _loc_4[2]);
trace("len:"+_loc_4.length);
trace("++++++++++++++++++++++++++++++++");
*/
_loc_7 = _loc_4.length;
while (_loc_7 < 3)
{
// label
_loc_5[_loc_7 + 1] = 64;
_loc_7 = _loc_7 + 1;
}// end while
_loc_8 = 0;
while (_loc_8 < _loc_5.length)
{
// label
_loc_3 = _loc_3 + (param2 == true ? (BASE64_URL_CHARS.charAt(_loc_5[_loc_8])) : (BASE64_CHARS.charAt(_loc_5[_loc_8])));
_loc_8 = _loc_8 + 1;
}// end while
}// end while
return _loc_3;
}// end function
public static function decodeToByteArray(param1:String, param2:Boolean = false) : ByteArray
{
var _loc_3:ByteArray;
var _loc_4:Array;
var _loc_5:Array;
var _loc_6:uint;
var _loc_7:uint;
var _loc_8:uint;
_loc_3 = new ByteArray();
_loc_4 = new Array(4);
_loc_5 = new Array(3);
_loc_6 = 0;
while (_loc_6 < param1.length)
{
// label
_loc_7 = 0;
while (_loc_7 < 4 && _loc_6 + _loc_7 < param1.length)
{
// label
_loc_4[_loc_7] = param2 == true ? (BASE64_URL_CHARS.indexOf(param1.charAt(_loc_6 + _loc_7))) : (BASE64_CHARS.indexOf(param1.charAt(_loc_6 + _loc_7)));
_loc_7 = _loc_7 + 1;
}// end while
_loc_5[0] = (_loc_4[0] << 2) + ((_loc_4[1] & 48) >> 4);
_loc_5[1] = ((_loc_4[1] & 15) << 4) + ((_loc_4[2] & 60) >> 2);
_loc_5[2] = ((_loc_4[2] & 3) << 6) + _loc_4[3];
_loc_8 = 0;
while (_loc_8 < _loc_5.length)
{
// label
if (_loc_4[_loc_8 + 1] == 64)
{
break;
}// end if
_loc_3.writeByte(_loc_5[_loc_8]);
_loc_8 = _loc_8 + 1;
}// end while
_loc_6 = _loc_6 + 4;
}// end while
_loc_3.position = 0;
return _loc_3;
}// end function

Error 1180, Flash

So, the same college project as last time, and this time im having a trouble with undefined methods. The entirity of my code is below
package
{
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class Lesson_05 extends MovieClip
{
private static const boardWidth:uint = 4;
private static const boardHeight:uint = 2;
private static const cardHorizontalSpacing:Number = 52;
private static const cardVerticalSpacing:Number = 52;
private static const boardOffsetX:Number = 171;
private static const boardOffsetY:Number = 148;
private var firstCard:Card;
private var secondCard:Card;
private var cardsLeft:uint;
var startPage:StartPage_1;
var matchPage:MatchPage_1;
var guessPage:GuessPage_1;
var startMessage:String;
var mysteryNumber:uint;
var currentGuess:uint;
var guessesRemaining:uint;
var guessesMade:uint;
var gameStatus:String;
var gameWon:Boolean;
public function Lesson_05():void
{
startPage = new StartPage_1();
matchPage = new MatchPage_1();
guessPage = new GuessPage_1();
addChild(startPage);
startPage.matchButton.addEventListener(MouseEvent.CLICK,onMatchButtonClick);
startPage.guessButton_1.addEventListener(MouseEvent.CLICK,onGuessButtonClick_1);
guessPage.matchButton.addEventListener(MouseEvent.CLICK,onMatchButtonClick_Guess);
//Output Errors #2025 when added this line;
guessPage.startButton.addEventListener(MouseEvent.CLICK,onStartButtonClick);
matchPage.startButton.addEventListener(MouseEvent.CLICK,onStartButtonClick_Match);
matchPage.guessButton_1.addEventListener(MouseEvent.CLICK,onGuessButtonClick_Match_1);
}
function onMatchButtonClick(event:MouseEvent):void
{
addChild(matchPage);
removeChild(startPage);
match();
}
function onGuessButtonClick_1(event:MouseEvent):void
{
addChild(guessPage);
removeChild(startPage);
guess();
}
function onMatchButtonClick_Guess(event:MouseEvent):void
{
addChild(matchPage);
removeChild(guessPage);
match();
}
function onStartButtonClick(event:MouseEvent):void
{
addChild(startPage);
removeChild(guessPage);
}
function onStartButtonClick_Match(event:MouseEvent):void
{
addChild(startPage);
removeChild(matchPage);
}
function onGuessButtonClick_Match_1(event:MouseEvent):void
{
addChild(guessPage);
removeChild(matchPage);
guess();
}
function guess():void
{
startMessage = "I am thinking of a number between 1 and 20";
mysteryNumber = Math.ceil(Math.random()*20);
guessesRemaining = 10;
guessesMade = 0;
gameStatus = "";
gameWon = false;
guessPage.output_txt.text = startMessage;
guessPage.input_txt.text = "";
guessPage.input_txt.backgroundColor = 0xFFCCCCCC;
guessPage.input_txt.restrict = "0-9";
guessPage.stage.focus = guessPage.input_txt;
guessPage.guessButton_2.enabled = true;
guessPage.guessButton_2.alpha = 1;
guessPage.againButton_1.visible = false;
guessPage.guessButton_2.addEventListener(MouseEvent.CLICK,onGuessButtonClick_2);
}
function onGuessButtonClick_2(event:MouseEvent):void
{
guessesRemaining--;
guessesMade++;
gameStatus = "Guesses Remaining: " + guessesRemaining + ", GuessesMade:" + guessesMade;
currentGuess = uint(guessPage.input_txt.text);
if (currentGuess > mysteryNumber)
{
guessPage.output_txt.text = "That's too high!" + "\n" + gameStatus;
checkGameOver();
}
else if (currentGuess < mysteryNumber)
{
guessPage.output_txt.text = "That's too low!" + "\n" + gameStatus;
checkGameOver();
}
else
{
//guessPage.output_txt.text = "Well Done! You got it!";
gameWon = true;
endGame();
}
function checkGameOver():void
{
if (guessesRemaining < 1)
{
endGame();
}
}
function endGame():void
{
if (gameWon)
{
guessPage.output_txt.text = "Yes, it's " + mysteryNumber + "!" + "\n" + "It only took you " + guessesMade + " guesses!";
}
else
{
guessPage.output_txt.text = "Sorry, you've run out of guesses!" + "\n" + "The correct number was " + mysteryNumber + ".";
}
guessPage.guessButton_2.removeEventListener(MouseEvent.CLICK,onGuessButtonClick_2);
guessPage.guessButton_2.enabled = false;
guessPage.guessButton_2.alpha = 0.5;
guessPage.againButton_1.visible = true;
guessPage.againButton_1.addEventListener(MouseEvent.CLICK,onAgainButtonClick_1);
}
function onAgainButtonClick_1(event:MouseEvent):void
{
guess();
guessPage.againButton_1.removeEventListener(MouseEvent.CLICK,onAgainButtonClick_1);
}
function match():void
{
var cardlist:Array = new Array();
for (var i:uint=0; i<boardWidth*boardHeight/2; i++)
{
cardlist.push(i);
cardlist.push(i);
}
cardsLeft = 0;
for (var x:uint=0; x<boardWidth; x++)
{
for (var y:uint=0; y<boardHeight; y++)
{
var c:Card = new Card();
c.stop();
c.x = x * cardHorizontalSpacing + boardOffsetX;
c.y = y * cardVerticalSpacing + boardOffsetY;
var r:uint = Math.floor(Math.random() * cardlist.length);
c.cardface = cardlist[r];
cardlist.splice(r,1);
c.addEventListener(MouseEvent.CLICK,clickCard);
addChild(c);
cardsLeft++;
}
}
}
function clickCard(event:MouseEvent)
{
var thisCard:Card = (event.target as Card);
if (firstCard ==null)
{
firstCard = thisCard;
firstCard.gotoAndStop(thisCard.cardface+2);
}
else if (firstCard ==thisCard)
{
firstCard.gotoAndstop(1);
firstCard = null;
}
else if (secondCard == null)
{
secondCard = thisCard;
secondCard.gotoAndStop(thisCard.cardface+2);
if (firstCard.cardface == secondCard.cardface)
{
removeChild(firstCard);
removeChild(secondCard);
firstCard = null;
secondCard = null;
cardsLeft -= 2;
if (cardsLeft ==0)
{
}
}
}
else
{
firstCard.gotoAndStop(1);
secondCard.gotoAndStop(1);
secondCard = null;
firstCard = thisCard;
firstCard.gotoAndStop(thisCard.cardface+2);
}
}
}
}
}
and the problems I'm getting are:
1180: Call to a possibly undefined method match.
I have very little knowledge with AS3, and have spent more than 2 hours trying to resolve this problem. Thanks in advance guys, PS any links to tutorials etc are massively appreciated
You misplaced close braces'}' of the function onGuessButtonClick_2. Use the below code and check it.
package
{
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class Lesson_05 extends MovieClip
{
private static const boardWidth:uint = 4;
private static const boardHeight:uint = 2;
private static const cardHorizontalSpacing:Number = 52;
private static const cardVerticalSpacing:Number = 52;
private static const boardOffsetX:Number = 171;
private static const boardOffsetY:Number = 148;
private var firstCard:Card;
private var secondCard:Card;
private var cardsLeft:uint;
var startPage:StartPage_1;
var matchPage:MatchPage_1;
var guessPage:GuessPage_1;
var startMessage:String;
var mysteryNumber:uint;
var currentGuess:uint;
var guessesRemaining:uint;
var guessesMade:uint;
var gameStatus:String;
var gameWon:Boolean;
public function Lesson_05():void
{
startPage = new StartPage_1();
matchPage = new MatchPage_1();
guessPage = new GuessPage_1();
addChild(startPage);
startPage.matchButton.addEventListener(MouseEvent.CLICK,onMatchButtonClick);
startPage.guessButton_1.addEventListener(MouseEvent.CLICK,onGuessButtonClick_1);
guessPage.matchButton.addEventListener(MouseEvent.CLICK,onMatchButtonClick_Guess);
//Output Errors #2025 when added this line;
guessPage.startButton.addEventListener(MouseEvent.CLICK,onStartButtonClick);
matchPage.startButton.addEventListener(MouseEvent.CLICK,onStartButtonClick_Match);
matchPage.guessButton_1.addEventListener(MouseEvent.CLICK,onGuessButtonClick_Match_1);
}
function onMatchButtonClick(event:MouseEvent):void
{
addChild(matchPage);
removeChild(startPage);
match();
}
function onGuessButtonClick_1(event:MouseEvent):void
{
addChild(guessPage);
removeChild(startPage);
guess();
}
function onMatchButtonClick_Guess(event:MouseEvent):void
{
addChild(matchPage);
removeChild(guessPage);
match();
}
function onStartButtonClick(event:MouseEvent):void
{
addChild(startPage);
removeChild(guessPage);
}
function onStartButtonClick_Match(event:MouseEvent):void
{
addChild(startPage);
removeChild(matchPage);
}
function onGuessButtonClick_Match_1(event:MouseEvent):void
{
addChild(guessPage);
removeChild(matchPage);
guess();
}
function guess():void
{
startMessage = "I am thinking of a number between 1 and 20";
mysteryNumber = Math.ceil(Math.random()*20);
guessesRemaining = 10;
guessesMade = 0;
gameStatus = "";
gameWon = false;
guessPage.output_txt.text = startMessage;
guessPage.input_txt.text = "";
guessPage.input_txt.backgroundColor = 0xFFCCCCCC;
guessPage.input_txt.restrict = "0-9";
guessPage.stage.focus = guessPage.input_txt;
guessPage.guessButton_2.enabled = true;
guessPage.guessButton_2.alpha = 1;
guessPage.againButton_1.visible = false;
guessPage.guessButton_2.addEventListener(MouseEvent.CLICK,onGuessButtonClick_2);
}
function onGuessButtonClick_2(event:MouseEvent):void
{
guessesRemaining--;
guessesMade++;
gameStatus = "Guesses Remaining: " + guessesRemaining + ", GuessesMade:" + guessesMade;
currentGuess = uint(guessPage.input_txt.text);
if (currentGuess > mysteryNumber)
{
guessPage.output_txt.text = "That's too high!" + "\n" + gameStatus;
checkGameOver();
}
else if (currentGuess < mysteryNumber)
{
guessPage.output_txt.text = "That's too low!" + "\n" + gameStatus;
checkGameOver();
}
else
{
//guessPage.output_txt.text = "Well Done! You got it!";
gameWon = true;
endGame();
}
}
function checkGameOver():void
{
if (guessesRemaining < 1)
{
endGame();
}
}
function endGame():void
{
if (gameWon)
{
guessPage.output_txt.text = "Yes, it's " + mysteryNumber + "!" + "\n" + "It only took you " + guessesMade + " guesses!";
}
else
{
guessPage.output_txt.text = "Sorry, you've run out of guesses!" + "\n" + "The correct number was " + mysteryNumber + ".";
}
guessPage.guessButton_2.removeEventListener(MouseEvent.CLICK,onGuessButtonClick_2);
guessPage.guessButton_2.enabled = false;
guessPage.guessButton_2.alpha = 0.5;
guessPage.againButton_1.visible = true;
guessPage.againButton_1.addEventListener(MouseEvent.CLICK,onAgainButtonClick_1);
}
function onAgainButtonClick_1(event:MouseEvent):void
{
guess();
guessPage.againButton_1.removeEventListener(MouseEvent.CLICK,onAgainButtonClick_1);
}
function match():void
{
var cardlist:Array = new Array();
for (var i:uint=0; i<boardWidth*boardHeight/2; i++)
{
cardlist.push(i);
cardlist.push(i);
}
cardsLeft = 0;
for (var x:uint=0; x<boardWidth; x++)
{
for (var y:uint=0; y<boardHeight; y++)
{
var c:Card = new Card();
c.stop();
c.x = x * cardHorizontalSpacing + boardOffsetX;
c.y = y * cardVerticalSpacing + boardOffsetY;
var r:uint = Math.floor(Math.random() * cardlist.length);
c.cardface = cardlist[r];
cardlist.splice(r,1);
c.addEventListener(MouseEvent.CLICK,clickCard);
addChild(c);
cardsLeft++;
}
}
}
function clickCard(event:MouseEvent)
{
var thisCard:Card = (event.target as Card);
if (firstCard ==null)
{
firstCard = thisCard;
firstCard.gotoAndStop(thisCard.cardface+2);
}
else if (firstCard ==thisCard)
{
firstCard.gotoAndstop(1);
firstCard = null;
}
else if (secondCard == null)
{
secondCard = thisCard;
secondCard.gotoAndStop(thisCard.cardface+2);
if (firstCard.cardface == secondCard.cardface)
{
removeChild(firstCard);
removeChild(secondCard);
firstCard = null;
secondCard = null;
cardsLeft -= 2;
if (cardsLeft ==0)
{
}
}
}
else
{
firstCard.gotoAndStop(1);
secondCard.gotoAndStop(1);
secondCard = null;
firstCard = thisCard;
firstCard.gotoAndStop(thisCard.cardface+2);
}
}
}
}
Hope it helps.
Your match() function is inside the onGuessButtonClick_2(MouseEvent) function.
So when you try it from anywhere else that is not the inside of the onGuessButtonClick_2 function, flash gets confused because it cannot find the function match() because it is only callable inside the onGuessButtonClick_2() function.
1180 errors are caused by Flash being dead.
1180 Error: Flash is Dead, please convert your code to HTML5 or do it natively.
okthxbai
Adobe

Accessing the .name property in a click handler

I am trying to make a simple card matching game via flash + actionscript, and am having major trouble with assigning event listeners and name. I have got all the card generation statements in place, and they all draw onto my stage, but even though I assigning their instance name with newCard.name, the name I get when tracing the click is always "root1" on every single button, and I have no idea why.
package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
public dynamic class cardGameMain extends MovieClip {
public function cardGameMain() {
addCards();
}
public function addCards() {
var lastCard:int;
for (var i = 1; i < 17; i++) {
var newCard:MovieClip;
newCard = new cardBackSymbol();
newCard.name = "card" + i;
addChild(newCard);
newCard.addEventListener(MouseEvent.MOUSE_UP, decideCard);
if (i == 1 || i == 5 || i == 9 || i == 13) {
newCard.x = 20;
if (i == 1) {
newCard.y = 20;
}
else if (i == 5) {
newCard.y = 240;
}
else if (i == 9) {
newCard.y = 460;
}
else if (i == 13) {
newCard.y = 680;
}
lastCard = 20;
} else if (i > 1 && i < 5) {
newCard.x = lastCard + 145;
newCard.y = 20;
lastCard = lastCard + 145;
} else if (i > 5 && i < 9) {
newCard.x = lastCard + 145;
newCard.y = 240;
lastCard = lastCard + 145;
} else if (i > 9 && i < 13) {
newCard.x = lastCard + 145;
newCard.y = 460;
lastCard = lastCard + 145;
} else {
newCard.x = lastCard + 145;
newCard.y = 680;
lastCard = lastCard + 145;
}
trace(newCard.name + " position is " + newCard.x + ", " + newCard.y);
}
}
public function decideCard(e:MouseEvent):void {
trace(this.name)
}
}
}
Any help on the matter is MUCH appretiated!
You're using the this keyword which is referring to the containing class, not the object clicked.
Try this instead:
public function decideCard(e:MouseEvent):void {
trace(DisplayObject(e.currentTarget).name)
}

AS3 creating dynamic Loader Names in a loop

EDITED for clarity: I want to load all kinds of images from an external url in a for loop.
I want to call them in a loop and when it's over I start the loop again. however I don't want to call them again with an "http request" rather I would like to loop through the loaded images over and over again.
Is it possible to create a dynamic Loader name in a loop?
Here is the code example:
var Count = 0;
// Loop Begins
var Count:Loader = new Loader();
Count.load(new URLRequest("myurl");
addChild(Count);
Count++;
// End Loop
Another example would be to have a NAME and just add the number on the end. But how
var Count = 0;
// Loop Begins
var MyName+Count:Loader = new Loader();
MyName+Count.load(new URLRequest("myurl");
addChild(MyName+Count);
Count++;
// End Loop
IN SHORT:
I want to load a bunch of images into an Array and loop through the loaded images by calling them later.
Thanks so much!
CASE1 code is how to load images in Sequence.
CASE2 code is how to load images in Synchronized.
First, the URL you want to import all images must be named sequentially.
for example Must be in the following format:
www.myURL.com/img0.jpg
www.myURL.com/img1.jpg
www.myURL.com/img2.jpg
www.myURL.com/img3.jpg
www.myURL.com/img4.jpg
www.myURL.com/img5.jpg
.
.
.
Try the code below, just a test.
CASE1
var imgLoader:Loader;
var imgRequest:URLRequest;
var count:int = -1;
const TOTAL_COUNT:int = 10;
var imgBox:Array = [];
var isCounting:Boolean;
function loadImage():void
{
count ++;
isCounting = true;
imgLoader = new Loader();
imgRequest = new URLRequest();
imgRequest.url = "www.myURL.com/img" + count +".jpg";
imgLoader.load(imgRequest);
imgLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, unloadedImg);
imgLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadedImg);
if(count == TOTAL_COUNT)
{
isCounting = false;
count = -1;
}
}
function onLoadedImg(e:Event):void
{
imgLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onLoadedImg);
var bmp:Bitmap = e.currentTarget.content;
bmp.x = Math.random() * width;
bmp.y = Math.random() * height;
bmp.width = 100;
bmp.height = 100;
this.addChild(bmp);
imgBox.push(bmp);
if( isCounting == false)
{
return;
}
loadImage();
}
function unloadedImg(e:IOErrorEvent):void
{
imgLoader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, unloadedImg);
trace("load Failed:" + e);
}
loadImage();
CASE2
var imgLoader:Loader;
var imgRequest:URLRequest;
const TOTAL_COUNT:int = 10;
var imgBox:Array = [];
function loadImage2():void
{
for(var i:int = 0; i<TOTAL_COUNT; i++)
{
imgLoader = new Loader();
imgRequest = new URLRequest();
imgRequest.url = "www.myURL.com/img" + i +".jpg";
imgLoader.load(imgRequest);
imgLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, unloadedImg);
imgLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadedImg);
}
}
function onLoadedImg(e:Event):void
{
imgLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onLoadedImg);
var bmp:Bitmap = e.currentTarget.content;
bmp.x = Math.random() * width;
bmp.y = Math.random() * height;
bmp.width = 100;
bmp.height = 100;
this.addChild(bmp);
imgBox.push(bmp);
}
function unloadedImg(e:IOErrorEvent):void
{
imgLoader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, unloadedImg);
trace("load Failed:" + e);
}
loadImage2();
If you want access loaded Image. refer a following code. If you do not put them in an array can not be accessed in the future.
for(var int:i=0; i<TOTAL_COUNT; i++)
{
var bitmap:Bitmap = imgBox[i] as Bitmap;
trace("index: " + i + "x: " + bitmap.x + "y: " + bitmap.y, "width: " + bitmap.width + "height: " + bitmap.height);
}
Now that we're on the same page:
var imgArray:Array = new Array;
var totalImages:int = 42;
var totalLoaded:int = 0;
var loaded:Boolean = false;
function loadImages():void{
for(var count:int = 0; count < totalImages; count++){
var image:Loader = new Loader();
image.load(new URLRequest("image" + i + ".jpg");
image.addEventListener(Event.COMPLETE, loaded);
imgArray.push(image);
}
}
function loaded(e:Event):void{
totalLoaded++;
if (totalLoaded == totalImages){
loaded = true;
}
}
function displayImages():void{
if (loaded){
for(var i:int = 0; i < imgArray.length(); i++){
addChild(imgArray[i]);
}
}
}

Getting 'TypeError' response for Gmail Meter Script.

I'm getting the following error:
"TypeError: Cannot read property "0.0" from null. (line227)"
I've been looking through the documentation here: https://developers.google.com/apps-script/articles/gmail-stats#section1
But haven't been able to troubleshoot.
Line227:
variables.dayOfEmailsReceived[day]++;
Sorry, new to stackoverflow but any help greatly appreciated.
Reference:
var BATCH_SIZE = 50;
var ss = SpreadsheetApp.getActiveSpreadsheet();
// If you are using several email addresses, list them in the following variable
// - eg 'romain.vialard#gmail.com,romain.vialard#example.com'
var aliases = 'romain.vialard#euromed-management.com';
function activityReport() {
var status = ScriptProperties.getProperty("status");
if (status == null) {
// If the script is triggered for the first time, init
init_();
}
else {
status = Utilities.jsonParse(status);
var previousMonth = new Date(new Date().getFullYear(), new Date().getMonth() - 1, 1).getMonth();
if (status == null || (status.customReport == false && status.previousMonth != previousMonth)) {
init_();
fetchEmails_(status.customReport);
}
// If report not sent, continue to work on the report
else if (status.reportSent == "no") {
fetchEmails_(status.customReport);
}
}
}
function fetchEmails_(customReport) {
var variables = Utilities.jsonParse(ScriptProperties.getProperty("variables"));
if (!customReport) {
var query = "after:" + variables.year + "/" + (variables.previousMonth) + "/31";
query += " before:" + new Date().getYear() + "/" + (variables.previousMonth + 1) + "/31";
}
else {
var query = "after:" + Utilities.formatDate(new Date(variables.startDate), variables.userTimeZone, 'yyyy/MM/dd');
query += " before:" + Utilities.formatDate(new Date(variables.endDate), variables.userTimeZone, 'yyyy/MM/dd');
}
query += " in:anywhere -label:sms -label:call-log -label:chats -label:spam -filename:ics";
query += " -from:maestro.bounces.google.com -from:unified-notifications.bounces.google.com -from:docs.google.com";
query += " -from:group.calendar.google.com -from:apps-scripts-notifications#google.com";
query += " -from:sites.bounces.google.com -from:noreply -from:notify -from:notification";
var startDate = new Date(variables.startDate).getTime();
var endDate = new Date(variables.endDate).getTime();
var conversations = GmailApp.search(query, variables.range, BATCH_SIZE);
variables.nbrOfConversations += conversations.length;
var sheets = ss.getSheets();
var people = sheets[0].getDataRange().getValues();
var record = [];
for (var i = 0; i < conversations.length; i++) {
Utilities.sleep(1000);
var conversationId = conversations[i].getId();
var firstMessageSubject = conversations[i].getFirstMessageSubject();
var starred = false;
if (conversations[i].hasStarredMessages()) {
variables.nbrOfConversationsStarred++;
starred = true;
}
var important = false;
if (conversations[i].isImportant()) {
variables.nbrOfConversationsMarkedAsImportant++;
important = true;
}
var location = "";
var labels = conversations[i].getLabels();
var nbrOfLabels = labels.length;
if (nbrOfLabels == 0) {
if (conversations[i].isInInbox()) {
variables.nbrOfConversationsInInbox++;
location += "Inbox,";
}
else if (conversations[i].isInTrash()) {
variables.nbrOfConversationsInTrash++;
location += "Trashed,";
}
else {
variables.nbrOfConversationsArchived++;
location = "Archived";
}
}
else {
variables.nbrOfConversationsInLabels++;
for (var j = 0; j < nbrOfLabels; j++) {
location += labels[j].getName() + ",";
}
}
var youReplied = false;
var youStartedTheConversation = false;
var someoneAnswered = false;
var messages = conversations[i].getMessages();
var nbrOfMessages = messages.length;
variables.nbrOfEmailsPerConversation[nbrOfMessages]++;
for (var j = 0; j < 10; j++) {
if (variables.topThreads[j][1] < nbrOfMessages) {
variables.topThreads.splice(j, 0, [firstMessageSubject, nbrOfMessages]);
variables.topThreads.pop();
j = 10;
}
}
var timeOfFirstMessage = 0;
var waitingTime = 0;
for (var j = 0; j < nbrOfMessages; j++) {
var process = true;
var date = messages[j].getDate();
var month = date.getMonth();
if (customReport) {
if (date.getTime() < startDate || date.getTime() > endDate) {
process = false;
}
}
else {
if (month != variables.previousMonth) {
process = false;
}
}
if (process) {
//////////////////////////////////
// Fetch sender of each emails
//////////////////////////////////
var from = messages[j].getFrom().replace(/"[^"]*"/g,'');
if (from.match(/</) != null) {
from = from.match(/<([^>]*)/)[1];
}
var time = Utilities.formatDate(date, variables.userTimeZone, "H");
var day = Utilities.formatDate(date, variables.userTimeZone, "d") - 1;
// Use function from Utilities file
variables = countSendsPerDaysOfWeek_(variables, date, from);
var body = messages[j].getBody();
// Use function from Utilities file
var resultsFromCalcMessagesLength = calcMessagesLength_(variables, body, from);
variables = resultsFromCalcMessagesLength[0];
var messageLength = resultsFromCalcMessagesLength[1];
var cc = messages[j].getCc().replace(/"[^"]*"/g,'').split(/,/);
for (var k = 0; k < cc.length; k++) {
if (cc[k].match(/</) != null) {
cc[k] = cc[k].match(/<([^>]*)/)[1];
}
}
var reg = new RegExp(from, 'i');
if ((variables.user + aliases).search(reg) != -1) {
if (j == 0) {
youStartedTheConversation = true;
timeOfFirstMessage = date.getTime();
}
if (j > 0 && !youStartedTheConversation) {
if (!youReplied) {
youReplied = true;
// Use function from Utilities file
variables = calcWaitingTime_(variables, date, timeOfFirstMessage, youStartedTheConversation);
}
}
variables.nbrOfEmailsSent++;
variables.timeOfEmailsSent[time]++;
variables.dayOfEmailsSent[day]++;
if (customReport) {
variables.monthOfEmailsSent[month]++;
}
var sharedWithTheOutsideWorld = false;
var to = messages[j].getTo().replace(/"[^"]*"/g,'').split(/,/);
for (var k = 0; k < to.length; k++) {
if (to[k].match(/</) != null) {
to[k] = to[k].match(/<([^>]*)/)[1];
}
if (to[k].search(variables.companyname) == -1){
sharedWithTheOutsideWorld = true;
}
var found = false;
for (var l = 0; l < people.length; l++) {
if (to[k] == people[l][0]) {
people[l][2]++;
found = true;
}
}
if (!found) {
people.push([to[k], 0, 1]);
}
}
if(!sharedWithTheOutsideWorld){
variables.sharedInternally++;
}
}
else {
if (j == 0) {
timeOfFirstMessage = date.getTime();
}
else if (youStartedTheConversation && !someoneAnswered) {
someoneAnswered = true;
// Use function from Utilities file
variables = calcWaitingTime_(variables, date, timeOfFirstMessage, youStartedTheConversation);
}
var found = false;
for (var k = 0; k < people.length; k++) {
if (from == people[k][0]) {
people[k][1]++;
found = true;
}
}
if (!found) {
people.push([from, 1, 0]);
}
var to = messages[j].getTo().replace(/"[^"]*"/g,'');
var checkSendToYou = false;
var aliasesTemp = new Array(variables.user).concat(aliases.split(','));
for(var k = 0; k < aliasesTemp.length; k++){
if(aliasesTemp[k] != ''){
var reg = new RegExp(aliasesTemp[k], 'i');
if (to.search(reg) != -1) {
checkSendToYou = true;
}
}
}
if(checkSendToYou)variables.sentDirectlyToYou++;
var sharedWithTheOutsideWorld = false;
to = to.split(/,/);
for (var k = 0; k < to.length; k++) {
if (to[k].match(/</) != null) {
to[k] = to[k].match(/<([^>]*)/)[1];
}
if (to[k].search(variables.companyname) == -1){
sharedWithTheOutsideWorld = true;
}
}
if(sharedWithTheOutsideWorld == false && from.search(variables.companyname) != -1){
variables.sharedInternally++;
}
variables.nbrOfEmailsReceived++;
variables.timeOfEmailsReceived[time]++;
variables.dayOfEmailsReceived[day]++;
if (customReport) {
variables.monthOfEmailsReceived[month]++;
}
}
if (to != null) {
to = to.toString();
}
if (cc != null) {
cc = cc.toString();
}
var dayOfWeek = Utilities.formatDate(date, variables.userTimeZone, "EEEE");
record.push([date, dayOfWeek, firstMessageSubject, from, to, cc, messageLength, location]);
}
}
if (youStartedTheConversation) {
variables.nbrOfConversationsStartedByYou++;
}
if (youReplied) {
variables.nbrOfConversationsYouveRepliedTo++;
}
}
variables.range += BATCH_SIZE;
ScriptProperties.setProperty("variables", Utilities.jsonStringify(variables));
sheets[0].getRange(1, 1, people.length, 3).setValues(people);
if (record[0] != undefined && sheets[1].getMaxRows() < 38000) {
sheets[1].getRange(sheets[1].getLastRow() + 1, 1, record.length, record[0].length).setValues(record);
}
if (conversations.length < BATCH_SIZE) {
sendReport_(variables);
}
}
It's a timezone issue. Go to your Gmail Meter file on your Google Drive account and then:
File -> Spreadsheet settings...
Change the timezone to your current time zone. I assume your Gmail account changes timezones automatically, but not your spreadsheet.
The error indicates that and day is 0 variables.dayOfEmailsReceived is null. It's not possible to refer to an index of null. variables.dayOfEmailsReceived is created in the init_() function, so check that you have that function defined and are using it correctly.