AS3 What's wrong with this code? - actionscript-3

Most of this is working and even with the error, it continues to work in part:
// Make PostItNote MC draggable and keep ON TOP
mcMXredBox.addEventListener(MouseEvent.MOUSE_DOWN, fl_ClickToDrag);
var q:int = 1;
// Add multiple copies of PostItNote when used
function fl_ClickToDrag(event:MouseEvent):void
{
event.currentTarget.parent.startDrag();
event.currentTarget.parent.parent.setChildIndex(DisplayObject(event.currentTarget.parent),87);
var my_PIN = new postItNote();
my_PIN.name = "my_RTC" + q; // Doesn;t like this line
this.parent.addChild(my_PIN);
trace("my_PIN = " + my_PIN);
this.my_PIN[q].x = 1388.05; // Doesn't like this line
this.my_PIN[q].y = 100;
q++;
}
The error is
TypeError: Error #1010: A term is undefined and has no properties.
at postItNote/fl_ClickToDrag()[postItNote::frame1:71]"

my_PIN.name = "my_RTC" + q; // Doesn;t like this line
This means that name is not a property of the postItNote class.
this.my_PIN[q].x = 1388.05; // Doesn't like this line
You seem to be using the wrong syntax here. What you want is probably just:
my_PIN.x = 1388.05; // In this context, my_PIN already refers to the movie clip named '"my_RTC" + q'

Related

Read a string in AS3

I have a question regarding to my project which is how to read a string in AS3.
Actually, I have an text file named test.txt. For instance:
It consists of:
Sun,Mon,Tue,Wed,Thu,Fri,Sat
and then I want to put all of them into an array and then a string to show them in the dynamic text Box called text_txt:
var myTextLoader:URLLoader = new URLLoader();
myTextLoader.addEventListener(Event.COMPLETE, onLoaded);
function onLoaded(e:Event):void
{
var days:Array = e.target.data.split(/\n/);
var str:String;
stage.addEventListener(MouseEvent.CLICK, arrayToString);
function arrayToString(e:MouseEvent):void
{
for (var i=0; i<days.length; i++)
{
str = days.join("");
text_txt.text = str + "\n" + ";"; //it does not work here
}
}
}
myTextLoader.load(new URLRequest("test.txt"));
BUT IT DOES NOT show them in different line and then put a ";" at the end of each line !
I can make it to show them in different line, but I need to put them in different line in txt file and also I still do not get the ";" at the end of each line unless put it in the next file also at the end of each line.
And then I want to read the string and show an object from my library based on each word or line. for example:
//I do not know how to write it or do we have a function to read a string and devide it to the words after each space or line
if (str.string="sun"){
show(obj01);
}
if (str.string="mon"){
show(obj02);
}
I hope I can get the answer for this question.
Please inform me if you can not get the concept of the last part. I will try to explain it more until you can help me.
Thanks in advance
you must enable multiline ability for your TextField (if did not)
adobe As3 DOC :
join() Converts the elements in an array to strings, inserts the
specified separator between the elements, concatenates them, and
returns the resulting string. A nested array is always separated by a
comma (,), not by the separator passed to the join() method.
so str = days.join(""); converts the Array to a single string, and as your demand ( parameter passed to join is empty "") there is no any thing between fetched lines. and text_txt.text = str + "\n" + ";"; only put a new line at the end of the text once.
var myTextLoader:URLLoader = new URLLoader();
var days:Array;
myTextLoader.addEventListener(Event.COMPLETE, onLoaded);
function onLoaded(e:Event):void
{
days = e.target.data.split(/\n/);
var str:String;
stage.addEventListener(MouseEvent.CLICK, arrayToString);
}
myTextLoader.load(new URLRequest("test.txt"));
function arrayToString(e:MouseEvent):void
{
text_txt.multiline = true;
text_txt.wordWrap = true;
text_txt.autoSize = TextFieldAutoSize.LEFT;
text_txt.text = days.join("\n");
}
also i moved arrayToString out of onLoaded
for second Question: to checking existance of a word, its better using indexOf("word") instead comparing it with "==" operator, because of invisible characters like "\r" or "\n".
if (str.indexOf("sun") >= 0){
show(obj01);
}
if (str.indexOf("mon") >= 0){
show(obj02);
}
Answer to the first part:
for (var i=0; i<days.length; i++)
{
str = days[i];
text_txt.text += str + ";" + "\n";
}
I hope I understand you correctly..
I wrote from memory, sorry for typos if there are...
For the second part, add a switch-case
switch(str) {
case "sun":
Show(??);
break;
.
.
.
}

How to access an object in AS3

I wrote this code
var enemies:Object = new Object();
// HP MP ATK DEF MATK MDEF AGI LUCK
enemies.Goblin = [40, 20, 6, 6, 3, 3, 4, 1];
which contains those stats for the goblin and I created a function that should take the stats from enemies.Goblin and put them in some variables but it won't work.
function createEnemy(enemyName:String):void {
e_hp = enemies.enemyName[0];
e_mp = enemies.enemyName[1];
e_atk = enemies.enemyName[2];
e_def = enemies.enemyName[3];
e_matk = enemies.enemyName[4];
e_mdef = enemies.enemyName[5];
e_agi = enemies.enemyName[6];
e_luck = enemies.enemyName[7];
}
This is the output error when the createEnemy function is executed: TypeError: Error #1010: A term is undefined and has no properties.
Object "enemies" does not have "enemyName" property.
Try this:
enemies[enemyName][0]
enemies[enemyName][1]
...
The answer had been given but what are you doing is a wrong way to do. Accessing properties by index is asking for trouble in a very near future.
It is better to do with classes but since you're using objects, I will try use objects too:
var goblin_stats:Object = { hp:40, mp:20, atk:6, def:6 }; // and so on
var elf_stats:Object = { hp:35, mp:30, atk:8, def:4 }; // and so on
...
// add as much characters as needed
Now I believe you just want to create a fresh goblin based on goblin stats. Just pass the stats to the createEnemy function:
createEnemy(goblin_stats);
function createEnemy(stats:Object):void {
e_hp = stats.hp;
e_mp = stats.mp;
// and so on
}
or better:
function createEnemy(stats:Object):void {
for (var property:String in stats) e_stats[property] = stats[property];
}
Store objects (everything) in arrays for easy referencing. Here are the key code:
var aEnemies:Array = new Array();
var mcEnemy:Object = new Object();
mcEnemy.iHP = 40; // set iHP property to 40
aEnemies.push(mcEnemy); // add enemy to array of enemies
trace("enemy 0's HP: " + aEnemies[0].iHP);

Type Error #1009 in flash cs6 using AS3

I am actually trying to check whether a game's score is a high score. And then if it is, the score will be added to the leaderboard.
However I got this error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at FlashGame_fla::MainTimeline/checkBananaHS()
In my game, in that particular frame, this is the code that would link to the checkBananaHS():
function rpslsWon():void
{
gameOverBananaMC.visible = true;
bananaWinLose.visible = true;
bananaWinLose.text = "Kampai " + cPlayerData.pName + "! You are totally bananas!! \nYour Score: " + pBananaScore;
toMenuBtn.visible = true;
rollAgainBtn.visible = true;
toMenuBtn.addEventListener(MouseEvent.CLICK, Click_backtoMain);
rollAgainBtn.addEventListener(MouseEvent.CLICK, backtoBanana);
saveItBtn.addEventListener(MouseEvent.CLICK, checkBananaHS);
cPlayerData.pBananaScore = pBananaScore;
saveData();
tracePlayerData();
}
And this is the piece of code in the high score's frame:
var rpslsHighScore:int;
var rpslsHSName:String;
rpslsHighScore = 0;
rpslsHSName = "";
//rpslsHighScore = 0;
bananaWinnerDisplay.text = " ";
bananaScoreDisplay.text = "0";
function checkBananaHS(event:MouseEvent):void
{
if ((cPlayerData.pBananaScore > rpslsHighScore ||
rpslsHighScore == 0) && cPlayerData.pBananaScore > 0)
{
trace("There's a new high score for Banana");
rpslsHighScore = cPlayerData.pBananaScore;
rpslsHSName = cPlayerData.pName;
bananaScoreDisplay.text = "" + rpslsHighScore;
bananaWinnerDisplay.text = rpslsHSName;
saveData();
}
}
I just can't manage to fix the error. Can anyone help me out? Thanks alot!
One of your variables within checkBananaHS() are not set when it is called. So when you try to access a property of that object, it errors out because nothing exists.
Wit that in mind, that means one of the following objects are not yet set in your function:
cPlayerData
bananaScoreDisplay
bananaWinnerDisplay
Run a trace on each of those, one at a time at the beginning of your function, and see which one doesn't return [Object object]

AS3 load variables from a txt file which has && formatting

I'm trying to load a txt file of variables into my AS3 project. The problem I have though seems to be down to the fact that the txt file (which is pre formatted and cannot be changed) is formatted using double amphersands... e.g.
&name=mark&
&address=here&
&tel=12345&
I'm using the following code to load the txt file
myLoader.addEventListener(Event.COMPLETE, onLoaded, false, 0, true);
myLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
urlRqSend = new URLRequest(addressToTxt.txt);
public function onLoaded(e:Event):void {
trace(myLoader.data);
}
Using URLLoaderDataFormat.VARIABLES generates the following error:
Error: Error #2101: The String passed to URLVariables.decode() must be a URL-encoded query string containing name/value pairs.
If I use URLLoaderDataFormat.TEXT I can load the data successfully but I'm not able (or don't know how to) access the variables.
Would anyone have any ideas or work arounds to this please.
Thanks,
Mark
I had that kind of problem some time ago.
I suggest you to load first as a text, remove those line breaks, the extra amphersands and parse manually:
var textVariables:String;
var objectVariables:Object = new Object();
...
myLoader.addEventListener(Event.COMPLETE, onLoaded, false, 0, true);
myLoader.dataFormat = URLLoaderDataFormat.TEXT;
urlRqSend = new URLRequest(addressToTxt.txt);
public function onLoaded(e:Event):void {
textVariables = myLoader.data;
textVariables = textVariables.split("\n").join("").split("\r").join(""); // removing line breaks
textVariables = textVariables.split("&&").join("&"); // removing extra amphersands
var params:Array = textVariables.split('&');
for(var i:int=0, index=-1; i < params.length; i++)
{
var keyValuePair:String = params[i];
if((index = keyValuePair.indexOf("=")) > 0)
{
var key:String = keyValuePair.substring(0,index);
var value:String = keyValuePair.substring(index+1);
objectVariables[key] = value;
trace("[", key ,"] = ", value);
}
}
}
I wrote that code directly here, I don't have any AS3 editor here, so, maybe you'll find errors.
If you have data in String and it has a structure just like you wrote, you can do a workaround:
dataInString = dataInString.split("\n").join("").split("\r").join(""); // removing EOL
dataInString = dataInString.slice(0,-1); // removing last "&"
dataInString = dataInString.slice(0,1); // removing first "&"
var array:Array = dataInString.split("&&");
var myVariables:Object = new Object();
for each(var item:String in array) {
var pair:Array = item.split("=");
myVariables[pair[0]] = pair[1];
}
That should make you an object with proper variables.

AS3 Is this code creating a MC Variable?

I've created some MC dynamicallly and did what I thought would be assigning values to variables in the MC's as I generated them e.g.
my_mc.name = "mc" + i + j;
trace("^^^^^^^^^^^^^^****************" + my_mc.name); // Works
my_mc.mcRow = j + 1; // Thinking I'm assigning values to a variable
trace("^^^^^^^^^^^^^^****************" + my_mc.mcRow); // Works
addChild(my_mc);
So, the trace outputs do what I expect, however, when I try to use/output the mcRow values later, they do not show up e.g.
var my_FC_row = (root as DisplayObjectContainer).getChildAt(r).name; // Works
var cxmy_FC_row = [my_FC_row].mcRow; // No value- does not work
var my_FC_name = (root as DisplayObjectContainer).getChildAt(r).name; // Works
var my_FC_x = (root as DisplayObjectContainer).getChildAt(r).x; // Works
var my_FC_y = (root as DisplayObjectContainer).getChildAt(r).y; // Works
cellData[r] = [my_FC_name, my_FC_x, my_FC_y, cxmy_FC_row];
trace("$$$$$$$$$$$$$$$$$$$$$ :" + r +" : "+ cellData[r]);
This code is in another function but I thought that the MC would still hold the value for mcRow.
What have I done/assumed incorrectly?
try this
var my_FC_row = (root as DisplayObjectContainer).getChildAt(r); // Works
var cxmy_FC_row = my_FC_row.mcRow; // Works