as3 Changing dynamic text in a symbol - actionscript-3

I have a symbol tile with multiple keyframes. One of the keyframes has a dynamic text box with the instance name pTwo.
I want to change the text in pTwo to an empty string and have tried:
pTwo.text = String(""); //Atempt One
MovieClip.pTwo.text = String(""); //Attempt two
Any help would be appreciated. Thanks in advance
EDIT: Im making a tile game and using an array to make the map - my array is as follows:
public var myMap: Array = [
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 3, 2, 2, 2, 0, 1],
[1, 0, 0, 0, 0, 0, 2, 0, 0, 1],
[1, 0, 2, 0, 2, 0, 0, 0, 0, 1],
[1, 0, 2, 2, 0, 3, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 2, 2, 0, 1],
[1, 0, 2, 0, 2, 0, 2, 0, 0, 1],
[1, 0, 2, 2, 2, 0, 2, 0, 2, 1],
[1, 0, 0, 0, 0, 0, 2, 0, 0, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
];
All the arrays are made from the symbol called 'tile' which consists of different key frames. So each number in the array references a different key frame. I have a character and when the character walks over the tile- I want the dynamic text to change to empty to imitate an empty tile- alternatively if theres a way to replace the tile with a [0] , that would also be good.
my code that generates the board:
var mapWidth = 10;
var mapHeight = 10;
var tileSide = 32;
var totalTiles = mapWidth * mapHeight;
var myMap: Array = [
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 3, 2, 2, 2, 0, 1],
[1, 0, 0, 0, 0, 0, 2, 0, 0, 1],
[1, 0, 2, 0, 2, 0, 0, 0, 0, 0],
[1, 0, 2, 2, 0, 3, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 2, 2, 0, 1],
[1, 0, 2, 0, 2, 0, 2, 0, 0, 1],
[1, 0, 2, 2, 2, 0, 2, 0, 2, 1],
[1, 0, 0, 0, 0, 0, 2, 0, 0, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
];
for (var i: int = 0; i < mapHeight; i++) {
for (var u: int = 0; u < mapWidth; u++) {
var cell: MovieClip = new tile();
cell.gotoAndStop(myMap[i][u] + 1);
cell.x = tileSide * u;
cell.y = tileSide * i;
addChild(cell);
};
};
Sorry i was not clear from the start. New at actionscript so i apologise in advance if my terminology is unclear.

Is this "tile" symbol available in all frames? If the playhead is moving, then the symbols within the MovieClip may become dereferenced. Add the "pTwo" object to your Watch list in debug and determine if it object exists as the "tile" symbol plays.
You may need to create a single frame symbol with 2 layers. One with the animated symbol and the other with the text.

If the TextField only exists on a particular frame, and you're sure that it's the current frame, you can use this:
TextField(getChildByName("pTwo")).text = "";
If you're not always sure, just wrap it in a try block:
try {
TextField(getChildByName("pTwo")).text = "";
} catch(error:Error) {
trace("text field doesn't exist on this frame");
}
If pTwo lives inside of a Movieclip, the code might look something like this:
try {
TextField(YourMovieclipsName.getChildByName("pTwo")).text = "";
} catch(error:Error) {
trace("text field doesn't exist on this frame");
}
Hope this helps!
Edit based on new info:
for (var i: int = 0; i < mapHeight; i++) {
for (var u: int = 0; u < mapWidth; u++) {
var cell: MovieClip = new tile();
cell.gotoAndStop(myMap[i][u] + 1);
cell.x = tileSide * u;
cell.y = tileSide * i;
try {
TextField(cell.getChildByName("pTwo")).text = "";
} catch(error:Error) {
trace("text field doesn't exist on this frame");
}
addChild(cell);
};
};

Related

How to embed a google chart (a html file) into a html webpage in adaptive width and height

I just learned to use google chart api to draw chart as follows:
Demo chart
The chart is now an html file named "demo_chart.html" whose code is:
<html>
<head>
<meta charset="utf-8">
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript", charset="utf-8">
google.charts.load("current", {packages:["corechart"]});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('number','Annual Compensation');
data.addColumn('number','count');
data.addColumn({type:"string",role:"tooltip", 'p':{'html': true}});
var a = [[10, 1, '0.3%'], [12, 2, '0.9%'], [14, 2, '1.6%'], [16, 4, '2.8%'], [18, 5, '4.3%'], [20, 14, '8.7%'], [22, 22, '15.5%'], [24, 40, '28.0%'], [26, 15, '32.6%'], [28, 28, '41.3%'], [30, 26, '49.4%'], [32, 22, '56.2%'], [34, 15, '60.9%'], [36, 17, '66.1%'], [38, 9, '68.9%'], [40, 17, '74.2%'], [42, 5, '75.8%'], [44, 14, '80.1%'], [46, 7, '82.3%'], [48, 3, '83.2%'], [50, 16, '88.2%'], [52, 6, '90.1%'], [54, 4, '91.3%'], [56, 3, '92.2%'], [58, 1, '92.5%'], [60, 3, '93.5%'], [62, 6, '95.3%'], [64, 2, '96.0%'], [66, 0, '96.0%'], [68, 1, '96.3%'], [70, 2, '96.9%'], [72, 1, '97.2%'], [74, 0, '97.2%'], [76, 0, '97.2%'], [78, 0, '97.2%'], [80, 1, '97.5%'], [82, 0, '97.5%'], [84, 1, '97.8%'], [86, 0, '97.8%'], [88, 0, '97.8%'], [90, 1, '98.1%'], [92, 0, '98.1%'], [94, 0, '98.1%'], [96, 0, '98.1%'], [98, 0, '98.1%'], [100, 1, '98.4%'], [102, 0, '98.4%'], [104, 0, '98.4%'], [106, 0, '98.4%'], [108, 0, '98.4%'], [110, 2, '99.1%'], [112, 0, '99.1%'], [114, 0, '99.1%'], [116, 0, '99.1%'], [118, 0, '99.1%'], [120, 1, '99.4%'], [122, 0, '99.4%'], [124, 0, '99.4%'], [126, 0, '99.4%'], [128, 0, '99.4%'], [130, 0, '99.4%'], [132, 0, '99.4%'], [134, 0, '99.4%'], [136, 0, '99.4%'], [138, 0, '99.4%'], [140, 1, '99.7%'], [142, 0, '99.7%'], [144, 0, '99.7%'], [146, 0, '99.7%'], [148, 0, '99.7%'], [150, 1, '100.0%']];
var b = HTML_format(a);
data.addRows(b);
var options = {
title: 'Demo Column Chart',
legend: { position: 'none' },
tooltip: {isHtml: true}
};
var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
function HTML_format(a)
{
var b = [];
var scale = a[1][0] - a[0][0];
for(var i=0; i<a.length; i++)
{
var text;
if(i < a.length - 1)
{
text = "demo";
}
else
{
text = "demo";
}
b.push([a[i][0], a[i][1], text]);
}
return b;
}
</script>
</head>
<body>
<div id="chart_div" style="width: 900px; height: 500px;"></div>
</body>
</html>
I want to embed the chart into another webpage and used iframe:
<tbody><tr><td class ='x text-center' style='align-items:center'><iframe src='demo_chart.html' style='width:900px;height:500px; border:0;max-width:100%' scrolling='no'></iframe></td><td class ='y' style=''><iframe src='demo_chart.html' style='width:900px;height:500px; border:0; max-width:100% ' scrolling='no'></iframe></td></tr></tbody>
It works well in my desktop but looks ugly in the mobile:
mobile_chart.
Could anyone help me to figure out how to correctly embed the html file so that it could be appropriately shown regardless of the width?
your problem seems to be that there is something wrong with your visualization options. You need to set the chartArea property to left:0 in your options.
After messing around with the properties I came up with these options: chartArea: {left: 0,top: 25}
so your options should look like:
var options =
{
title: 'Demo Column Chart',
legend: { position: 'none' },
tooltip: {isHtml: true},
chartArea: {left: 0,top: 25}
};
I would also suggest adding margin: auto; to your chart div to add the white space left on the main file:
<div id="chart_div" style="width: 900px; height: 500px;margin: auto;"></div>
so your demo_chart.html file should look something like this:
demo_chart.html
<html>
<head>
<meta charset="utf-8">
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript", charset="utf-8">
google.charts.load("current", {packages:["corechart"]});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('number','Annual Compensation');
data.addColumn('number','count');
data.addColumn({type:"string",role:"tooltip", 'p':{'html': true}});
var a = [[10, 1, '0.3%'], [12, 2, '0.9%'], [14, 2, '1.6%'], [16, 4, '2.8%'], [18, 5, '4.3%'], [20, 14, '8.7%'], [22, 22, '15.5%'], [24, 40, '28.0%'], [26, 15, '32.6%'], [28, 28, '41.3%'], [30, 26, '49.4%'], [32, 22, '56.2%'], [34, 15, '60.9%'], [36, 17, '66.1%'], [38, 9, '68.9%'], [40, 17, '74.2%'], [42, 5, '75.8%'], [44, 14, '80.1%'], [46, 7, '82.3%'], [48, 3, '83.2%'], [50, 16, '88.2%'], [52, 6, '90.1%'], [54, 4, '91.3%'], [56, 3, '92.2%'], [58, 1, '92.5%'], [60, 3, '93.5%'], [62, 6, '95.3%'], [64, 2, '96.0%'], [66, 0, '96.0%'], [68, 1, '96.3%'], [70, 2, '96.9%'], [72, 1, '97.2%'], [74, 0, '97.2%'], [76, 0, '97.2%'], [78, 0, '97.2%'], [80, 1, '97.5%'], [82, 0, '97.5%'], [84, 1, '97.8%'], [86, 0, '97.8%'], [88, 0, '97.8%'], [90, 1, '98.1%'], [92, 0, '98.1%'], [94, 0, '98.1%'], [96, 0, '98.1%'], [98, 0, '98.1%'], [100, 1, '98.4%'], [102, 0, '98.4%'], [104, 0, '98.4%'], [106, 0, '98.4%'], [108, 0, '98.4%'], [110, 2, '99.1%'], [112, 0, '99.1%'], [114, 0, '99.1%'], [116, 0, '99.1%'], [118, 0, '99.1%'], [120, 1, '99.4%'], [122, 0, '99.4%'], [124, 0, '99.4%'], [126, 0, '99.4%'], [128, 0, '99.4%'], [130, 0, '99.4%'], [132, 0, '99.4%'], [134, 0, '99.4%'], [136, 0, '99.4%'], [138, 0, '99.4%'], [140, 1, '99.7%'], [142, 0, '99.7%'], [144, 0, '99.7%'], [146, 0, '99.7%'], [148, 0, '99.7%'], [150, 1, '100.0%']];
var b = HTML_format(a);
data.addRows(b);
var options = {
title: 'Demo Column Chart',
legend: { position: 'none' },
tooltip: {isHtml: true},
chartArea: {left: 0,top: 25}
};
var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
function HTML_format(a)
{
var b = [];
var scale = a[1][0] - a[0][0];
for(var i=0; i<a.length; i++)
{
var text;
if(i < a.length - 1)
{
text = "demo";
}
else
{
text = "demo";
}
b.push([a[i][0], a[i][1], text]);
}
return b;
}
</script>
</head>
<body>
<div id="chart_div" style="width: 900px; height: 500px;margin: auto;"></div>
</body>
</html>
This should fix your problem.

pmap data format (Julia 1.1.0)

I have a function which I run using pmap. I would like the output to be a vector - p - whose entries are the output of each iteration run parallel. For illustration, the code is as below:
#everywhere function addo(mc,mcf)
p = zeros(Int64,mcf) ;
p[mc] = mc ;
return p
end
pmap(mc -> addo(mc,mcf),1:1:mcf) ;
The output I get is
100-element Array{Array{Int64,1},1}:
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0 … 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 2, 0, 0, 0, 0, 0, 0, 0, 0 … 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 3, 0, 0, 0, 0, 0, 0, 0 … 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
⋮
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0 … 0, 0, 0, 0, 0, 0, 0, 98, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0 … 0, 0, 0, 0, 0, 0, 0, 0, 99, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0 … 0, 0, 0, 0, 0, 0, 0, 0, 0, 100]
Instead, I need something like this, p = [1,2,3,4,...,100];
How do I get the output in the desired format?
pmap, similar to map, applies your transformation function,mc -> addo(mc, mcf), to each element in the collection 1:1:mcf. The problem in your code is that for each element in the collection you are creating a new vector of size mcf and the overall result becomes an array of the arrays you create in each transformation, whereas what you want is the result of pmap to be of size mcf. Therefore, for each element in the collection, you should return only one element. pmap already gives you a collection grouping the results of each transformation.
#everywhere function addo(mc)
return mc
end
p = pmap(mc -> addo(mc), 1:1:mcf)
This will give you p = [1,2,3,4,...,100].

passing json data to chartjs

JSON.js
var Jsondata = {
"label1": 0,
"label2": 0,
"label3": 2,
"label4": 0,
"label5": 0
}
var labels = //?? have to get the labels from the Jsondata
var data = //?? have to get the numbers from the Jsondata
and i am using this to make a line graph using chartjs and the code is as follows.
type: 'line',
data: {
labels: labels //["label1","label2","label3","label4", "label5""],
datasets: [{
label:'',
fill: 'false',
data: data //[0, 3, 6, 4, 5,3,2],
You can use the built in Object helpers keys() and values():
var Jsondata = {
"label1": 0,
"label2": 0,
"label3": 2,
"label4": 0,
"label5": 0
}
var labels = Object.keys(Jsondata)
var data = Object.values(Jsondata)
labels:
["label1", "label2", "label3", "label4", "label5"]
data:
[0, 0, 2, 0, 0]

How to create Two Level JSON

How to create Two Level JSON, if i have to get data in multiple lists, like below:
Category's List > Item's List
For an example: Sony > LED TV, Laptop, Phones etc..
Earlier i have created single Level JSON,
For an example: LED TV, Laptop, Phones see below:
[
{
"ProductID":"1",
"ProductName":"LED TV"
},
{
"ProductID":"2",
"ProductName":"Laptop"
}
]
So here my question is how my JSON should look like ?
You can use any JSON "data type" as values. So here, you would create an object whose keys are the categories and the values are arrays of products:
{
"Sony": [{
"ProductID": "1",
"ProductName": "LED TV"
}, {
...
}],
"Panasonic": [...]
}
Instead of using an array of products, you could also use an object of object, keyed by produce ID. Optimize the structure for your use case, i.e. structure it in such a way that you can easily access the information you need.
See http://json.org/ for a complete syntax description.
yeah I agree with #FelixKling in one of my app i have used same kind of JSON:
{
"Mixed Platter" : [
{
"title" : "Veggie",
"description" : "Lorem ipsum dolor sit amet, conse adipiscing elit.",
"cost" : "5.25"
},
{
"title" : "Non Veggie",
"description" : "Lorem ipsum dolor sit amet, conse adipiscing elit.",
"cost" : "5.75"
}
],
"Soups" : [
{
"title" : "Mulagatawny Soup",
"description" : "Lorem ipsum dolor sit amet, conse adipiscing elit.",
"cost" : "3.75"
},
{
"title" : "Daal Soup",
"description" : "Lorem ipsum dolor sit amet, conse adipiscing elit.",
"cost" : "3.25"
}
]
}
[
{
"author": "anonymous",
"background": "0xaaaaaa",
"ball": "0xff1111",
"mat": "0xffff00",
"bouncer": "0xff00ff",
"obstacle": "0x00ddff",
"data":
[
[0, 0, 0, 0, 0],
[1, 1, 1, 1, 0],
[1, 1, 1, 1, 1],
[1, 1, 0, 0, 1],
[1, 1, 1, 1, 0],
[1, 0, 0, 1, 0],
[1, 0, 0, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 0],
[1, 0, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 0],
[1, 1, 0, 0, 0],
[1, 0, 1, 1, 0],
[1, 1, 1, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 1, 1, 1],
[1, 1, 1, 0, 1],
[0, 0, 0, 0, 1],
[1, 1, 1, 1, 1],
[1, 1, 0, 0, 0],
[0, 1, 1, 1, 0],
[0, 0, 0, 1, 1],
[1, 1, 1, 1, 0],
[1, 0, 0, 0, 0],
[1, 1, 1, 1, 1],
[0, 0, 0, 0, 1],
[1, 1, 1, 1, 0],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1]
]
}
]

Multiple HitTestObject ActionScript 3.0

I've been developing a video gamish thing in AS3. I have an array to draw a game field that contains road, fire, finish gate etc. Then, I add a MovieClip that is controled via mouse by player and try to check collisions with road MovieClip.
However, I does not work... It never traces "IN". I couldn't find any error in my code -But you never be sure...
Could you give a hand to solve this problem?
Thank y'all!
Here is the code:
Declarations:
public class Player extends MovieClip
{
public var player:MovieClip;
public var road:MovieClip;
public var finish:MovieClip;
public var fire:MovieClip;
public var sting:MovieClip;
public var map:Array = new Array();
Array initialization:
/* 1 ROAD
* 2 FINISH
* 3 FIRE
* 4 STRING
*/
public function Player():void
{
map = [ [ 1, 1, 3, 3, 3, 3, 3, 3, 1, 1 ],
[ 1, 1, 3, 4, 4, 4, 4, 3, 1, 1 ],
[ 1, 1, 3, 4, 4, 4, 4, 3, 1, 1 ],
[ 1, 1, 3, 3, 3, 3, 3, 3, 1, 1 ],
[ 1, 1, 1, 1, 1, 1, 1, 1, 1, 2 ],
[ 1, 1, 1, 1, 1, 1, 1, 1, 1, 2 ],
[ 1, 1, 3, 3, 3, 3, 3, 3, 1, 1 ],
[ 1, 1, 3, 4, 4, 4, 4, 3, 1, 1 ],
[ 1, 1, 3, 4, 4, 4, 4, 3, 1, 1 ],
[ 1, 1, 3, 3, 3, 3, 3, 3, 1, 1 ]
];
// 10 x 10 array
Adding hitTest function:
addEventListener( Event.ENTER_FRAME, playerHitTest );
And hitTest function:
public function playerHitTest( e:Event ):void
{
if ( player.hitTestObject( road ) )
{
trace("IN");
}
}
PS: If I make the condition !player.hitTestObject( road ), it always traces "IN".
Thanks again!
There's not enough code here to see exactly what's going on but it's obvious that the hitTest will fail because you are only checking against one road object, but according to your map array there should be many road objects.
You will need an array to store the road objects:
var roadArray:Array = [];
Wherever you create the road objects you should also be inserting them into this array:
roadArray.push(road);
Then when you perform the hitTest you must loop through the entire array and check each road object for a collision:
public function playerHitTest( e:Event ):void
{
for each(var road in roadArray)
{
if ( player.hitTestObject( road ) )
{
trace("IN");
}
}
}