Can Someone Explain whats happening with overwriting variables - actionscript-3

var Minion = {};
var player = {};
function newGame():void
buildMinion();
buildPlayer();
trace(player.m1.minionName); // traces "Minion Two" ?!
}
I don't understand why (player.m1.minionName) is being overwritten to (player.m2.minionName). I am doing something wrong obviously and would like to understand what.
buildMinion():void
{
Minion.minionName = "Default";
}
buildPlayer():void
{
player.m1 = Minion;
player.m2 = Minion;
player.m1.minionName = "Minion One";
player.m1.minionName = "Minion Two";
}

player.m1.minionName = "Minion Two";
You have a typo here. This should be player.m2.minionName as pointed by #DodgerThud. However, even if you change this to m2 it will still not work as player.m1 and player.m2 both referring to same Minion object. So any change you do in one will be reflected in the other. You need to use separate object for them. Something like this:
var Minion1 = {};
var Minion2 = {};
player.m1 = Minion1;
player.m2 = Minion2;
Since Minion1 and Minion2 are both empty objects, you can get rid of those temporary variables if they are not needed anywhere else.
player.m1 = {}
player.m2 = {}

Please, read your code carefully
Change this
player.m1.minionName = "Minion One";
player.m1.minionName = "Minion Two";
to this
player.m1.minionName = "Minion One";
player.m2.minionName = "Minion Two";

Related

AS3 Flash How to make text fields required?

I want to know how to put restrictions in the fields like if the user didn't type anything or still has a blank field, then it should not submit but shows *required field or something like that. Examples would be great.
The codes. I don't know where to start from here
var fllname:TextField;
var address:TextField;
var ContactNo:TextField;
var quantity:TextField;
var otrack:TextField;
btnSubmit1.addEventListener(MouseEvent.CLICK, submit);
function submit(e:MouseEvent):void{
var urlvars: URLVariables = new URLVariables;
urlvars.fllname = fllname.text;
urlvars.Oadd = address.text;
urlvars.ContactNo = ContactNo.text;
urlvars.oquantiy = quantity.text;
urlvars.otrack = otrack.text;
urlvars.cake = txtCake.text;
urlvars.frosting = txtFrosting.text;
urlvars.topping = txtToppings.text;
urlvars.topping2 = txtToppings2.text;
urlvars.filling = txtFilling.text;
urlvars.amt = lblAmount.text;
var urlreq:URLRequest = new URLRequest("http://localhost/MCC/order.php");
urlreq.method = URLRequestMethod.POST;
urlreq.data = urlvars;
var loader : URLLoader = new URLLoader;
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
loader.load(urlreq);
nextFrame();
}
You can use the enabled parameter to control if your btnSubmit1 is clickable, so you will probably need to start with is disabled.
btnSubmit1.enabled = false;
btnSubmit1.addEventListener(MouseEvent.CLICK, submit);
Next you will need to listen to the TextEvent.TEXT_INPUT event on all your TextFields
fllname.addEventListener(TextEvent.TEXT_INPUT,paramChanged);
address.addEventListener(TextEvent.TEXT_INPUT,paramChanged);
//etc etc
This will notify you any-time the user changes their value, you can then create a single function (or a function for each control) to test the values, once they pass you can re-enable the submit button.
function paramChanged(event:TextEvent):void
{
if (fllname.text != "" && address.text != "")//add your other fields here
{
btnSubmit1.enabled = true;
}
else
{
btnSubmit1.enabled = false;//If something changes that means we now fail the test you will want to disable the button again
}
}
You could make custom test functions for each field as needed.

Passing variables through navigateURL to open iFrame as3

By using NavigateURL I can easily pass variables as below through Flash to paypal, this works no problem and can include all the data required.
var req:URLRequest = new URLRequest("https://www.paypal.com/cgi-bin/webscr");
var reqVars:URLVariables = new URLVariables();
reqVars.cmd = "_xclick-subscriptions";
reqVars.business = "BUSINESS CODE";
reqVars.lc = "GR";
reqVars.item_name = "Product Name";
reqVars.item_number = "Product Number 0001";
reqVars.no_note = "1";
reqVars.no_shipping = "2";
reqVars.src = "1";
reqVars.a3 = "15.00";
reqVars.p3 = "1";
reqVars.t3 = "Y";
reqVars.currency_code = "EUR";
//and so on
req.data = reqVars;
req.method = URLRequestMethod.POST;
navigateToURL(req);
By using callIframe as shown below I can easily open an iFrame from Flash.
calliFrame("http://www.webAddress.com/" +"?iframe=true&width=800&height=550", "Page Title", "Page Description");
function calliFrame(url:String, title:String, desc:String):void{
if (ExternalInterface.available) {
trace("calling prettyPhoto");
try {
ExternalInterface.call('$.prettyPhoto.open', url, title, desc);
} catch (event:Error) {
trace("Error occurred!");
}
} else {
trace("External Interface unavailable");
}
}
Is it possible to pass the navigateURL variables through the calliFrame method? I've tried variations but not gotten to either transfer the data or show the page.
I hoped something like the example below would work but only get a blank page or list of the data shown in the iFrame:
calliFrame("https://www.paypal.com/cgi-bin/webscr" +reqVars +"?iframe=true&width=800&height=550", "Page Title", "Page Description");
Any help would be much appreciated, thanks in advance.
The second parameter is the window name, so all you gotta do is pass the name of your iframe.
public function navigateToURL(request:URLRequest, window:String = null):void
docs: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/package.html#navigateToURL()

as3 passing content of one text filed to another

I'm tying to pass the contents of
B1L_bnt.text
to
buddy_txt.text
Can anyone tell me what am doing Wong here?
function clickHandler(event:MouseEvent):void
{
/*container_mcM.removeEventListener(MouseEvent.CLICK,clickHandler);*/
var clickedObject:DisplayObject = event.target as DisplayObject;
var bobby = Object(root).littlepicker.B1L_bnt.text;
if (clickedObject.name == 'frd_bnt1')
{
Object(root).BFFwho.buddy_txt.text = "bobby";
Object(root).gotoAndPlay(15);
}
Try this:
function clickHandler(event:MouseEvent):void
{
/*container_mcM.removeEventListener(MouseEvent.CLICK,clickHandler);*/
var clickedObject:DisplayObject = event.target as DisplayObject;
var bobby = Object(root).littlepicker.B1L_bnt.text;
if (clickedObject.name == 'frd_bnt1')
{
Object(root).BFFwho.buddy_txt.text = bobby;
Object(root).gotoAndPlay(15);
}
The issue was that you were declaring a variable named bobby. When you were trying to set the text of buddy_txtyou were setting it to a literal String 'bobby' instead of the value of the variable.

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.

A conflict exists with definition newBox in namespace internal

function makeABox(e):void {
if (e.name == "seri1"){
var newBox:karo1 = new karo1();
}else if(e.name == "seri2"){
var newBox:karo2 = new karo2();
}else{
var newBox:zemin1 = new zemin1();
}
ust_bar.addChild(newBox);
newBox.x = i*60;
newBox.y = s*60;
}
Dee, you should make a question. I'm presuming you got problems with 'namespaces'. Try to define de variable first, with a superclass type, then in those conditionals just give a value. Like this:
function makeABox(e):void {
var newBox:somesuperclass;
if (e.name == "seri1") {
newBox = new karo1();
} else if (e.name == "seri2") {
newBox = new karo2();
} else {
newBox = new zemin1();
}
ust_bar.addChild(newBox);
newBox.x = i*60;
newBox.y = s*60;
}
This is actionsscript3? If is, you probably need e.currentTarget.name.
Hope this helps.