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

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.

Related

TypeError 1009, began appearing randomly after adding new button code

SOLVED: It was a two stop functions. Two. Stop. Functions. AS3 can be so annoying sometimes.
I am making a space invaders style game for a school assignment. I was adding in a button to go between frames and I deleted an unnecessary layer that contained a single sprite in my game, which I added back onto the main code layer. Then after attempting to restart my game to check the button, I began receiving a TypeError Code 1009. It is stating that there is an error with MainTimeline/moveBullet() and, MainTimeline/eFrame(). These sections of the code were not altered whatsoever during the creation of the button or movement of the sprite, however, as the bullet is linked to the moving sprite I think the error may be there. I'll put the code below, if anyone knows how to fix this specifically or has a general idea of how to do so please let me know. This is due in a few hours and would greatly appreciate a quick fix. Thanks.
Re-named sprites, re-typed code sections returning errors again. Most fixes appear to be in instance names, corrected them and no fix.
this.addEventListener(Event.ENTER_FRAME, eFrame)
function eFrame(e: Event) {
controls();
restrain();
moveBullet();
enemyHit();
barrierHit();
function moveBullet() {
if (bull.visible == true) {
bull.y -= bulletspeed;
if (bull.y < 0) {
bull.visible = false;
}
}
}
The game had been running fine until this point. The character would move along bottom and shoot up at "bad guys". Bullets were removing bad guys. Now I get the usual super fast run through frames repeatedly. NO Compiler Errors, just output, being
TypeError: Error #1009
"I deleted an unnecessary layer that contained a single sprite in my game, which I added back onto the main code layer. Then after attempting to restart my game to check the button, I began receiving a TypeError Code 1009."
Possible solutions:
(1)
Did you also give the Sprite an instance name? I suspect the name bull is what's expected.
(2)
Eliminate that the problem is in moveBullet(). Disable the call and if the error still exists, then the problem must be between variables bull and bulletspeed.
Comment the call movebullet() in function eFrame:
function eFrame(e: Event)
{
//moveBullet();
....

Errors when switching scenes in flash AS3

In my little Flash project, I use the Enter Frame Gameloop commands, and I use hittestobject and such then put that function in to the game loop. However, When switching scenes I am bombarded with errors because the objects included in the functions are not on screen anymore. My question is how can I either take those functions out of the game loop when changing from that specific scene, or write the code so it only includes that one specific scene. Like: if current frame = 2, or something in real code form. Thankyou so much I am very appreciative for any replies.
function gameLoop(evnt:Event){
try{
//Write your all codes
}catch(err:Error){
}
}
Use try/ catch.
I don't know the way you change scenes.
Based on your description,I guess the way you change scenes is to go to different frames.Am I right?
If I am right,I think you can declare a variable to mark if you need to do the hittestobject function or other operations.
such as:
gameloop(){
if(!changing)
a.hitTestObject(b)
}

functions in actionscript (addChild)

I've Just started learning actionscript 3.0, and I got a problem.
I just want a symbol to be in the stage for many times ,not in a same time, but it brings them all in at the beginning. I just used this piece of code below:
for (i=1,i<10,i++){
stuff();
}
function stuff():void {
theSymbol.x=(Math.random()*100);
addChild("theSymbol");
}
Is there any other ways to :
Bring the symbol into the stage many times "not in a same time" ,in a while.
Call a function many times without using "for".
Thanks for any Ideas .PLZ answer soon as possible!
;)
I believe you wanna do something like:
setInterval(stuff, 1000) // call stuff() once every second.
function stuff():void {
theSymbol.x=(Math.random()*100);
addChild("theSymbol");
}
Does it helps you?

leaving and revisting a scene keeping any user input changes the same. Can it be done?

Hi Im relatively new to flash developing and i have a quick question about saving user input. I have a maze scene whereby the user navagates a character around until confronted with another object, when the character hits the object a new scene is opened promting the user to pick a solution to a problem. Once the user clicks the correct answer a box appears saying return to the maze, however when clicked and returned to the maze the character starts back in its original postion, where as I would like the scene to resume where it left off, ie the character is at the point where it collided with the object, the object has dissappeared and the character can resume on the same course.
Thanks for giving this a read I hope it makes sense and some one has a solution for me.
I did have some nice images to explain it better but apprently i need 10 reputation points to upload those.
EDIT: First Id like to say thanks for the rep points you bunch of stars and secondly I know using scenes in flash is seriously cr*p practice and outdated but its the way I learnt all those years ago and seen as Flash itself will be outdated soon Im not really looking to learn another approach using sprites or frames, I just kind of want a fix for this way if poss thank you for answering!
EDIT: Wanting the red rectangle to be removed from the scene once the black square collides with it. It also takes you to the next frame upon doing so. here is my code.
addEventListener(Event.ENTER_FRAME, fl_EnterFrameHandler);
function fl_EnterFrameHandler (event:Event):void
{
if (player.hitTestObject(Risk))
{
removeEventListener(Event.ENTER_FRAME, fl_EnterFrameHandler);
removeChild(Risk);
nextFrame();
}
}
I'd suggest you forget about Scenes! They are old, buggy, bring loads of issues with code and are generally a Bad Pratice!! So if you are just learning AS3, dont learn with Scenes!!
Use MovieClips or Sprites instead. And just add and remove them as you need!
EDIT:
To ur edit ;)
and seen as Flash itself will be outdated soon
thats just plain false and a widespread misinformation. It just has a new purpose like MultiPlattform Game Development. But thats a whole different discussion.
You could solve this by saving the x,y coordinates and then restoring them. But i promise you, you will run in to alot more problems/bugs as you go allong!
Like saved Points and Time. Will you triger the Questionscene again when u place the player on the object(last Position). Save the answers, and so on ...
Changing to DisplayObjects will save you time in the end. Just saying ;)
EDIT2:
your code in the comment should look like this:
function fl_EnterFrameHandler (event:Event):void
{
if (player.hitTestObject(Risk))
{
removeEventListener(Event.ENTER_FRAME, fl_EnterFrameHandler);//remove to prevent errors if it fires again and there is no Object to hitTest
removeChild(Risk);// do what needs to be done on this frame
nextFrame();// and then move to the next
}
}
And following correct convetions and make everybody's life easier reading this, it would look like this!
function fl_EnterFrameHandler (event:Event):void
{
if (player.hitTestObject(risk))
{
removeEventListener(Event.ENTER_FRAME, fl_EnterFrameHandler);//remove to prevent errors if it fires again and there is no Onject to hitTest
removeChild (risk);
nextFrame ();
}
}
i'm probaly confusing u now, just use the top one ;)
EDIT3:
Ok, i just had a butchers at it. It's all on a single Frame now. I would have prefered Classes but that'lljust confuse you.
To add new qestions you just have to dublicate the Question MovieClip in the Library and change texts, leave instance names the same tho, then the code will work as is!
DOWLOAD
It is certainly possible. You would store the characters attributes (e.g. current position) within a variable. Then on returning to your game you would use this variable to set the starting position of your character.
I agree that scenes are not the ideal route you should be taking. Possibly reading a good AS3 book would save you hours/days/weeks in the long run.

hitTestObject, stopDrag stops drag on two movieclips even though function states one movieclip to stop drag

I have a function that states when movieclip1 is dragged and hits a line then it stops the drag, however it seems to stop the entire drag function in the swf on the other movieclips even though they arent called in the function. Can somebody please help me with this.
Regards
T
Here is the code:
function hitTest(event:Event):void
{
if (movieclip1.hitTestObject(line))
{
movieclip1.stopDrag();
}
else
{
}
}
Are you absolutely positive you only have one instance of movieclip1 on your stage? Definitely double check. Are you creating them dynamically, or are they preloaded when your SWF loads?
If they're preloaded:
Perhaps during testing you made some quick copies of it, and now those copies have the same name and they're all responding the same. That's my first guess.
If they're loaded dynamically:
Check the function where they're being created. If you're naming them in a loop (with a number on the end like the above), be sure that you're properly increasing the numeric value used on the end.