AS3 Event.COMPLETE passing argument - actionscript-3

I am trying to pass an argument on Event.Complete, so once they load the image, i can handle them accordingly of the position, store them, etc. See below the code and the output:
var pic:Array = ["https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png","https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png","https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png","https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png"];
for (var ii: uint = 0; ii < pic.length; ii++) {
var imageURLRequest:URLRequest = new URLRequest(pic[ii]);
var myImageLoader:Loader = new Loader();
myImageLoader.load(imageURLRequest);
trace ("the ii: " + ii);
myImageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, function(evt:Event)
{
doIt(evt, ii)
} , false, 0, true);
function doIt(evt:Event, msg:int) {
//var myBitmapData:BitmapData = new BitmapData(myImageLoader.width, myImageLoader.height);
//myBitmapData.draw(myImageLoader);
//var myBitmap:Bitmap = new Bitmap;
//myBitmap.bitmapData = myBitmapData;
trace ("message : " + msg);
}
}
/////Output
the ii: 0
the ii: 1
the ii: 2
the ii: 3
message : 4
message : 4
message : 4
message : 4
///Expected Output
/////Output
the ii: 0
the ii: 1
the ii: 2
the ii: 3
message : 0
message : 1
message : 2
message : 3
Thank for the help
Speego

As you might know, the Loader class inherits from DisplayObject which in turn means you have access to the .name property.
Having this in mind you can 'abuse' this property to store the value of your ii variable as a string and send it to the doIt() function as the second parameter - after casting it back to an integer.
So simply change this:
var myImageLoader:Loader = new Loader();
to this:
var myImageLoader:Loader = new Loader();
myImageLoader.name = ii.toString();
and the onComplete callback handler to this:
myImageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, function(evt:Event):void
{
doIt(evt, int(flash.display.LoaderInfo(evt.target).loader.name));
}, false, 0, true);
This should give you an output like this:
the ii: 0
the ii: 1
the ii: 2
the ii: 3
message : 1
message : 0
message : 3
message : 2

Related

How to take a specific info from a string in json format?

I've got this AS3 code :
var myString:String;
var request:URLRequest = new URLRequest("http://www.swellmap.co.nz/ajr.php?r=plugin&a=Surfing&s=Anse%20Vata&country=nz&swellmap=1&country=ncd&swellmap=1&_=1460963404274");
var loader:URLLoader = new URLLoader();
loader.load(request);
loader.addEventListener(Event.COMPLETE,weatherLoaded);
function weatherLoaded(e:Event):void{
myString = e.target.data;
trace(myString); //output is {"tides":"High: 05:40 am (1.32 m); Low: 12:10 pm (0.57 m); High: 06:10 pm (1.19 m); ","seatemp":"27°C","forecastdate":"17h","rating":"<img src='http:\/\/www.swellmap.co.nz\/style\/img\/weathericons\/1r.png' alt='Poor conditions' title='Poor conditions' \/>","rating_class":"<span class='badge badge-important' alt='Poor conditions' title='Poor conditions'>1<\/span>","summary":"<img class='wx-summary' src='http:\/\/www.swellmap.co.nz\/style\/img\/weathericons\/suncloud.png' title='Sunny with some cloud' \/>","title":"Anse Vata","smaplink":"http:\/\/www.swellmap.co.nz\/surfing\/new-caledonia\/anse-vata","vars":{"hs_sw":{"value":"0.4","title":"Swell","unit":"m"},"hs":{"value":"0.6","title":"Wave","unit":"m"},"wface":{"value":"0.8","title":"Set face","unit":"m"},"tp":{"value":"13","title":"Period","unit":"s"},"dpm":{"value":"S","title":"Swell dir","unit":"°"},"windma":{"value":"E 12","title":"Wind","unit":"kts"},"gstma":{"value":"16","title":"Gusts","unit":"kts"}}}
var myData : Object = JSON.parse(e.target.data);
for each (var s:* in myData) { trace("key:",s,"value:",myData[s]); }
trace(myData); }
My String is containing lots of infos.
How could I take specifics informations ?
Exemple:
If I want to take the swell (in this example, the swell is : "0.4 m # 13 s"). How could I do that? (the purpose is to displays it in a text box like that :
function(searchTheSwell){
var swell_AnseVata;
swell_AnseVata =.... ?
info_txt.text = swell_AnseVata;
}
Thx
Just set a breakpoint after you parse the data and examine the myData in the debugger - you will see the object structure. Or just trace the whole object structure out:
import mx.utils.ObjectUtil;
trace(ObjectUtil.toString(myData));
In your case you'd need to put your string together out of the vars in your object:
var hs_sw:Object = myData.vars.hs_sw;
var tp:Object = myData.vars.tp;
trace(hs_sw.value + " " + hs_sw.unit + " # " + tp.value + " " + tp.unit);

Make my AS3 code go fetch information on a website that I don't own

There is this website : http://www.swellmap.co.nz/ and I'd like to make my AS3 code go fetch some infos and displays it flash.
Is it possible if I don't own the website ?
Exemple :
I want to display these infos in my AS3 code. Is this possible ? How can I do ?
Thx for your help,
EDIT
Thx to the full answer of VC.One I've managed to paste infos in a String.
Here's what I did :
var myString:String;
var request:URLRequest = new URLRequest("http://www.swellmap.co.nz/ajr.php?r=plugin&a=Surfing&s=Anse%20Vata&country=nz&swellmap=1&country=ncd&swellmap=1&_=1460963404274");
var loader:URLLoader = new URLLoader();
loader.load(request);
loader.addEventListener(Event.COMPLETE,weatherLoaded);
function weatherLoaded(e:Event):void{
myString = e.target.data;
trace(myString); //output is {"tides":"High: 05:40 am (1.32 m); Low: 12:10 pm (0.57 m); High: 06:10 pm (1.19 m); ","seatemp":"27°C","forecastdate":"17h","rating":"<img src='http:\/\/www.swellmap.co.nz\/style\/img\/weathericons\/1r.png' alt='Poor conditions' title='Poor conditions' \/>","rating_class":"<span class='badge badge-important' alt='Poor conditions' title='Poor conditions'>1<\/span>","summary":"<img class='wx-summary' src='http:\/\/www.swellmap.co.nz\/style\/img\/weathericons\/suncloud.png' title='Sunny with some cloud' \/>","title":"Anse Vata","smaplink":"http:\/\/www.swellmap.co.nz\/surfing\/new-caledonia\/anse-vata","vars":{"hs_sw":{"value":"0.4","title":"Swell","unit":"m"},"hs":{"value":"0.6","title":"Wave","unit":"m"},"wface":{"value":"0.8","title":"Set face","unit":"m"},"tp":{"value":"13","title":"Period","unit":"s"},"dpm":{"value":"S","title":"Swell dir","unit":"°"},"windma":{"value":"E 12","title":"Wind","unit":"kts"},"gstma":{"value":"16","title":"Gusts","unit":"kts"}}}
}
Now, I didn't quite understand how could I retrieve only some infos (like the swell for exemple).
Is it possible to show me in AS3 code, how could I do that ? (in this exemple, we can see that the swell is "0.4 m # 13 s")
exemple of what I'd like to do :
function(searchTheSwell){
var swell_AnseVata;
swell_AnseVata =.... ?
info_txt.text = swell_AnseVata;
}
If you use the Developer Tools of your browser then you'll see that there's a PHP file being accessed to get the information. It starts http://www.swellmap.co.nz/ajr.php?r= and you need to find what it says for you. Now to view the contents just remove the part of the URL with &callback=XYZ where XYZ will be whatever the link has..
1)
Here's an example of how to get data for a location :
http://www.swellmap.co.nz/ajr.php?r=plugin&a=Surfing&s=ZZZZZ&country=nz&swellmap=1&country=YYY&swellmap=1&_=1460950764514
Replace &s=ZZZZZ with name of location, so if I want Anse Vata it becomes &s=Anse%20Vata and La Nera becomes &s=La%20Nera. So use %20 for any spaces in location name. Replace &country=YYY with example &country=fra.
2)
To get the JSON data for Anse Vata, declare your new String variable to later hold the JSON text and then just use URLStream in AS3 to load the link (open in browser tab to check contents) : http://www.swellmap.co.nz/ajr.php?r=plugin&a=Surfing&s=Anse%20Vata&country=nz&swellmap=1&country=ncd&swellmap=1&_=1460963404274
In the Event Complete listener function of URLStream you just use (example) myString = e.target.data;. Then use a JSON parser on your myString or do it manually yourself using String functions (like indexOf to word search).
3) If you opened the above link in an new tab you'll see the JSON entries you need to parse.
Swell : "hs_sw":{"value":"0.4","title":"Swell","unit":"m"} and for
extracting the # 13 s period use :
"tp":{"value":"13","title":"Period","unit":"s"}
Wind : "windma":{"value":"E 12","title":"Wind","unit":"kts"}
Icon : "summary":"<img class='wx-summary'
src='http:\/\/www.swellmap.co.nz\/style\/img\/weathericons\/suncloud.png'
You'll have to clean up the icon links so it becomes for example :
http://www.swellmap.co.nz/style/img/weathericons/suncloud.png
EDIT :
The code below extracts the expected information from the JSON string. Just replace &s= with any other location (eg: &s=Ilot%20Tenia) and it extracts the Swell, Wind and Icon URL entries...
var myURL : String = "http://www.swellmap.co.nz/ajr.php?r=plugin&a=Surfing&s=Anse%20Vata&country=nz&swellmap=1&country=ncd&swellmap=1&_=1460963404274";
var URL_Req : URLRequest = new URLRequest( myURL );
var URL_load:URLLoader = new URLLoader();
URL_load.addEventListener(Event.COMPLETE, completeHandler);
try { URL_load.load( URL_Req ); }
catch (error:Error)
{ trace("Could not load - Please check URL is correct : " + error.message); }
var idx_start : int = 0; var idx_end : int = 0;
var str_Swell : String = "";
var str_Swell_Period : String = "";
var str_Swell_Dir : String = "";
var str_Wind : String = ""; var url_Icon : String = "";
function completeHandler(e : Event):void
{
//var myData : Object = JSON.parse(e.target.data);
//for each (var s:* in myData) { trace("key:",s,"value:",myData[s]); }
var myString : String = String(e.target.data);
//trace ( "myString : " + myString);
//# Get Swell
idx_start = myString.indexOf("\"hs_sw\":" , 0 );
idx_end = myString.indexOf("," , idx_start + 1 );
str_Swell = myString.substring(idx_start + 18, idx_end-1);
str_Swell = str_Swell + " m";
//trace ("Swell : " + str_Swell );
//# Get Direction (for Swell)
idx_start = myString.indexOf("\"dpm\":" , 0 );
idx_end = myString.indexOf("," , idx_start + 1 );
str_Swell_Dir = myString.substring(idx_start + 16, idx_end-1);
//trace ("Swell dir : " + str_Swell_Dir );
//# Get time Period (for Swell)
idx_start = myString.indexOf("\"tp\":" , 0 );
idx_end = myString.indexOf("," , idx_start + 1 );
str_Swell_Period = myString.substring(idx_start + 15, idx_end-1);
str_Swell_Period = " # " + str_Swell_Period + " s";
//trace ("Period : " + string_Period );
//# Join Swell Direction, Size & Period entries into one sentence
str_Swell = str_Swell_Dir + " " + str_Swell + str_Swell_Period;
trace ("Swell : " + str_Swell );
//# Get Wind
idx_start = myString.indexOf("\"windma\":" , 0 );
idx_end = myString.indexOf("," , idx_start + 1 );
str_Wind = myString.substring(idx_start + 19, idx_end-1);
str_Wind = str_Wind + " kts";
trace ("Wind : " + str_Wind );
//# get Image URL
idx_start = myString.indexOf("\'wx-summary\'" , 0 );
idx_end = myString.indexOf("'" , idx_start + 18 );
url_Icon = myString.substring(idx_start + 18, idx_end);
url_Icon = url_Icon.replace(/\\/g, "");
trace ("URL : " + url_Icon );
//# load image using : new URLRequest (url_Icon);
}

Extjs 5 Uncaught TypeError: Cannot read property 'height' of undefined

I changed extjs 4.2.4 to 5.0.0 and I have this error.
Uncaught TypeError: Cannot read property 'height' of undefined
SubSectionColumn.js?_dc=1451369036732:262
loadSprites: function() {
var chart = Ext.getCmp(this.identifier);
//var xAxisPositionArray = [55,95,135,175];
var xAxisPositionArray = new Array();//[55,95,175];
var yAxisStart = 0;
var xAxisHeight = chart.surface.height
var sprite;
var fromPosition;
var toPosition;
var width;
var noOfBars;
var sectionWidth = 0;
var newWidthValue = 0;
var tmp;
var chart = Ext.getCmp(this.identifier);
var storeLen =this.storeValue.getCount();
var teststore =this.storeValue;
var maxVal = 0;
for (var i =0; i<storeLen; i++){
tmp = parseFloat(teststore.data.items[i].get('data1'));
if (tmp >maxVal){
maxVal=tmp;
}
}
if(maxVal>0){
width = this.widthValue;
noOfBars = this.storeLength;//5;
sectionWidth = parseInt(parseInt(width)/parseInt(noOfBars));
for(var j=0;j<noOfBars;j++){
newWidthValue = parseInt(newWidthValue) + parseInt(sectionWidth);
xAxisPositionArray[j] = newWidthValue;
}
var gridArray = this.gridLineArrayVal.split(",");
for(var i=0;i<xAxisPositionArray.length;i++){
fromPosition = parseInt(xAxisPositionArray[i]);
toPosition = fromPosition+1;
sprite = Ext.create('Ext.draw.Sprite', {
type: 'path',
path: "M"+fromPosition+" " + yAxisStart +"L"+toPosition+" "+xAxisHeight+" Z", //if the value is "M100 40 L150 40", it's ok.
"stroke-width": (gridArray[i]=='dotted' || gridArray[i]=='line')?"0.4":"0",
"stroke-dasharray":(gridArray[i]=='dotted')?"4,4,2.5,4,4,4":"",
//"stroke-dasharray":"20,20",
stroke: "#9f9f9f",
//style:{cursor: 'pointer'},
surface: chart.surface
});
sprite.show(true);
};
maxVal=parseFloat(maxVal)*(0.10)+parseFloat(maxVal); //Math.round(maxVal)+1;
chart.axes.getAt(0).maximum = maxVal;
}
}
chart.surface is obviously undefined, as that's where you're trying to access the height property.
In ExtJS 5, there were two big changes that are likely to be relevant here. First, they tightened up on the configuration properties. Many are now prefixed, and can not be accessed by their old names. Instead, you should use (and should have used, before) the accessor methods - e.g. chart.getSurface()
Second: The charting library used in ExtJs 4 was deprecated, and has been replaced with Sencha Charts. This brought in some significant API changes.
Without more details, it's hard to say what the root cause is, but the error is definitely to do with lack of access to the surface property.

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

Action Script 3 Errors 1061 and 1120

I'm new to this, not sure what I'm doing wrong, Thank you for help
Please see my code below:
protected function getXMLUrl(vid:String):String {
//return 'http://v.iask.com/v_play.php?vid='+ vid;
var rand:* = Math.random();
var f1:* = function (param1:Number) : Number {
var _loc_2:* = param1.toString(2);
var _loc_3:* = _loc_2.substring(0, _loc_2.length - 6);
return parseInt(_loc_3, 2);
};
var Str1:* = "Z6prk18aWxP278cVAH";
var Date1:* = new Date();
var Num:* = this.f1(int(Date1.time / 1000));
var str:* = vid.toString() + str1 + Num + rand.toString();
var hash:* = MD5.hash(str);
var encode:* = hash.substr(0, 16).toString() + Num.toString();
return 'http://v.iask.com/v_play.php?vid='+ vid + "&ran=" + rand + "&p=i&k=" + encode;
}
And here are the Errors I'm getting
1061: Call to a possibly undefined method f1 through a reference with static type
1120: Access of undefined property MD5.
1120: Access of undefined property str1.
Never mind, I fixed it
add
import com.adobe.crypto.MD5;
also deleted all the " :* "