AS3: simple if condition not working - actionscript-3

I hav this if condition:-
if(leo.blocking == true && leo.leftLimb.hitTestObject(hitList[i])==true){
}
its in an enterframe event and leo.blocking is false. but still it is entering the if brackets, ~_~ makes me cry!

If your conditional is obviously false, but it enters it...
Check if your swf is updated(not cached by ide/browser). Add something stupid like Alert with message or some color change, or a trace statement if you are debugging.
Sometimes watches are displayed incorrectly by ide(but I guess this is not your case, it usually happens for more complicated expressions). In that case you have to add trace for your expression to see the real value.

Do u have certain that leo.blocking is true? try to trace leo.blocking.
Doesn't make sense entering on the condition...or maybe leo.blocking changes sometimes.
Do you have more information? It's hard to help like this :)
If you please give me more code to work with I'll be glad to help.
Cheers!

Related

Can anyone explain .split(''); in Razor?

I sitting here with a ton of code (that works) which I have to look through, and some of it happens to be in Razor. Not too hard to understand, yet not something I've tried before.
My problem is that the function .split(''); (notice the '') comes up several times and I don't understand neither the purpose nor what it does, cause I feel like it would just give an error to split on nothing.
I can't really test anything as the code is way too large for me to be able to just run to check such a minor thing, and there are no real places to test Razor unless I get enough understanding to just make a testpage from scratch.
The most simple example I can find is this:
function openbysetting(settingcookie)
{
var settingsarray = settingcookie.split('');
for (var i = 0; i < settingsarray.length; i++)
{
if (settingsarray[i] == 'f')
{
....
}
};
}
Mind you, settingcookie isn't defined or mentioned anywhere else in this entire code, so I don't even understand why a regular split function would be required.
Edit: Never mind, I'm told it's a javascript thing. Just ignore the previous sentence.
As for a result, I'd expect it to just simply not work, but it seems to be doing fine.
C# just gives an error if you do it, and this is a C# based language after all.
Hope someone can clarify. Thank you.
Edit: Okay, I understand now. It just splits between every single character. Thanks for the help.

AS3 - How do I code a particular frame?

So its like I want a code just for frame 2. Is there any other way other than just going to frame 2 and typing in the Actions Panel? For example I am using a class called supportForce and I want a part of the code to apply only for frame 2. This might be stupid but just in order to get a doubt cleared I asked this question, forgive me if I am wasting your time. Thanks in advance.
Sure, simply write your code from the parent scope of supportForce inside a statement such as this one:
if(supportForce.currentFrame == 2) { //DO MAGIC ON FRAME 2! }

How do I check if objects in a frame are loaded yet or not after a gotoAndStop(5) action?

I am facing a problem, where there is a function executed the next line right after a gotoAndStop(130) action, the function depends in its work on the objects inside this frame. but the problem is that the function seems not seeing any children.. why? why? why? why?
myMc.gotoAndStop(130);
create_waypoints(par1,par2,par3);
the function creat_waypoints depends on the drawn obstacles that exists in frame 130 inside the myMc.
it seems (but I don't know for sure) by the time that creat_waypoints executes its actions, frame 130 was just entered but not yet loaded...
What can I do? Thanks in advance..
after many experiments, I have come to a working solution but it is twisted, it had to skip an entire loop of actions and I am not happy with it, but for now it is all what I have..
myMc.gotoAndStop(2);
myMc.addEventListener(Event.ENTER_FRAME,mc_ef,false,0,true);
function mc_ef(e:Event):void {
if (e.target.loaded==null) {
e.target.loaded=true;
} else {
e.target.loaded=null;
creat_waypoints(par1,par2,par3);
e.target.removeEventListener(e.type,arguments.callee);
}
}
A simpler work-around would be including the "obstacles" and their dependencies in earlier frame where the depending logic is written. They can be included as "out-of-stage" invisible members.

A simple move-left-and-right program with a bit of acceleration (ActionScript 3.0, similar to Java)

http://pastebin.com/ap6hVRVb
I've fixed everything I can see, but maybe I've looked at it too much and I've grown used to what is wrong in the code, therefore I will have trouble seeing what is incorrect.
Basically I just need a symbol called player to move left and right when I press either A or D or left or right arrow keys.
I'm Still learning code I know I'm pathetic.
At first glance I see
rightDown=false instead of ==. What problems are you having with it exactly?
You should really end your statements with a semi-colon, man. It also doesn't hurt to use constant variables to store the key codes, so you don't accidentally mistype one.
In the updateSpeed function you have, you might want to change the 'if()' statements to 'else if()' conditionals? I'm not completely sure if that's the problem, but I don't think it'd hurt to try.
Also, as #VBCPP said, there's a line that says
if(rightDown=false && leftDown==false){
When it should be
if (rightDown == false && leftDown == false){
OR
if ( !rightDown && !leftDown ){

as3 MouseEvent localX acting weird

I encountered this really weird situation, I have this bar and I addEventListener to it, so when
the bar's clicked, trace localX
private function _barClicked($e:MouseEvent):void {
trace($e.localX)
}
The weird thing is that, when clicking at the same spot, sometimes it jump to a wrong number
which I can't figure out why, I traced the width of the bar, and it's the right value, the localX is
just giving me random numbers. Has anyone ever ran into this problem? Thanks!
Hmmm, strange
I've tried a simple scenario where I have a rectangle named 'bar' and pasted your listener for the CLICK event, then tried the MOUSE_DOWN event. Both worked fine.
I didn't get random values.
My guess is your bar clip contains other objects inside and you might get values from children of bar, not bar itself. Not sure though, it's just a guess.
You could try to make sure that your values come from $e.currentTarget as $e.target might change depending on: your clip and how many children it holds, position of the click and event phase.
Try
private function _barClicked($e:MouseEvent):void {
trace($e.currentTarget.mouseX);
}
Hope it helps!
I was having the same problem, and Kevin Condon solution from the comment above helped my problem. I am just posting it here, because I almost missed it in the comment.
private function _barClicked($e:MouseEvent):void {
var coord:Point = $e.currentTarget.globalToLocal(new Point($e.stageX, $e.stageY));
trace(coord.x);
}
Thanks Kevin.
Just wanted to note a variation on this problem for those for whom the above is not working.
I was having the same issue - localX showing odd values. Mine weren't random, just far lower than what I expected. I.e., click on the far right of the sprite and get only 17.2, event though the width was reporting as 160.
What I found was that my instance was a scaled version of a symbol who's native width was much less.
Once I set it up so that the symbol and the instance had the same width, I started getting the right values.
Hope this helps someone.
If found that as long as I use the solution that Kevin Condon proposed differences in scaling between different symbols are taken into account and everything works out fine.