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

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.

Related

Actionscript 3 problems with "Call to a possibly undefined method"

Thanks partly to info from people here, I'm getting more comfortable with Actionscript 3, but I've got a problem that is very puzzling.
The program (done entirely in AS3, no Flash) has several different screens. One does music and it's working very well. Another does video. Obviously when somebody goes from music to video we need to make sure the music is turned off. There is a Main screen that handles going from one to the other.
I started out doing it this way, and the reason I got "Call to a possibly undefined method" is obvious. The following is in the Main class, with the "private var" part inside the class but external to the functions and the "music = new MusicPanel" part in one of the functions:
private var music:MusicPanel;
music = new MusicPanel(trackNames.songNames, trackNames.numSongs);
When switching to the video panel, I added a public function in MusicPanel called StopMusic and called it when the user went to the video panel:
if(music != null)
music.StopMusic();
That got the error:
Call to a possibly undefined method StopMusic
I was checking to make sure music was not null, but that error didn't seem like a bad thing. So I changed the code to:
private var music:MusicPanel = new MusicPanel();
and added a function that would get the song names and number of songs to the music class. That did not help- I got the same error, and in fact the function that tried to put the song names and number of songs got the error also.
At the same time, the Video panel does not give me that error, even though I have laid it out in exactly the same way.
private var video:VideoPanel = new VideoPanel;
video.PlayVideo();
I do a fair amount of setup on the music screen when it gets called as a new class, I do less setup for the video screen. I'm not sure if that makes a difference.
Clearly there is something I don't understand here. Anybody got any idea what's going on? I've looked at a number of questions about this, but have not found an answer. I think I'm doing this right, but the compiler thinks I'm doing it wrong, so I must be doing it wrong.
Later Note: One answer mentioned the difference between Sprite and MovieClip. I gave that a try, changing to MovieClip does not help, and the VideoPanel, which works, extends Sprite.
What is the base class of MusicPanel, Sprite or MovieClip? If Sprite, change to MovieClip and see what that does. In AS3 Sprites are not dynamic, so can't take properties that are not inherent in the class.
It appears that the problem is coming either from Actionscript 3 or from FlashDevelop. I built a new module (SongsPanel) which is very close to the same as the original MusicPanel. It works. If I add a public function in MusicPanel the compiler comes back with the same error. If I add a public function in SongsPanel everything works.
Does FlashDevelop keep track of errors in some hidden file? I'm guessing that's what's going on, and that there is a bug in the way that is done.
PITA!- but at least it now works.

Best Way To Implement Invincibility Frames in AS3

I'm making a little flash game and I want to implement invincibility frames, but I'm not exactly sure how to go about doing it. Essentially, when the player walks over an object, I want to remove a health (I know how to do this) and then have invincibility frames so that the player has time to move off of the object. Something along the lines of
if (player.hitTestObject(spikes)) {
//remove health
// INVINCIBILITY FRAMES
}
Any direction here would be helpful. Thanks!
You'd better give some details about your programming style, I mean is your programming style pure as3 or pure stage or using both as3 and stage.
I'm gonna answer your question assuming you're using pure as3 style.
If you want to make your hero invincible for everything instead of just that hitted spike, you should add a public method in your hero's ( or character's) class and a variable to keep track of how much time left to remove invincibility. Something like this
public function gotHit(){
invincibilityTimeLeft=500;//500 is miliseconds, which is a half second edit this as you wish.
isInvincible=true;
}
then in your game's Event.ENTER_FRAME loop, you should decrease your character's invincibilityTimeLeft according to passed time. When it reaches 0 or below again, you set isInvincible variable of your character as false. I hope you understand what I mean.
Best of lucks.
-Ozan

How do I create a simple on-mouse-click rotating object in flash

This question may get downvoted or go unanswered because it's not the greatest and incredible silly but anyway,
I'm taking an intro level Webanimation course this semester at UNI and had I know what their expectations are for our hand-in projects i would have never taken it up.
Basically the teacher taught us stuff to the extent of masking/tweening and very few mouse-event and basic function codes.
Now she is expecting us to make a god damn ENTIRE PIPE GAME. The one where there are a bunch of rotating pipes and you gotta rotate them in place before a timer runs out and then the water flows through them.
For this project I have to somehow figure out the following (even though she didn't teach any of this):
-creature a grid of rotate-able pipes (one mouse click I assume would do a 90 degree classic tween rotation of the object)
-creature some sort of logic hit-box value chain to make pipes decide when to fill with water (they fill with water (a.k.a turn blue inside as an animation) once they are connected to another water filled pipe, for example)
-creature multiple levels and a menu screen
-add a music track.
Now i know this site is for specific help only and you basically can't ask for help on an entire project, so for now if somebody could just help me out with the following:
How do I create a rotating pipe on mouseclick?
So I have my pipe movieclip created and I have my Mouse Event code ready but I don't have the faintest on how to make a tween within the pipe and connect it to the code so that it rotates on mouseclick.
So this far, let's say for one of the pipes, instance pipe_1, I want to do this:
pipe_1.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler);
function fl_MouseClickHandler(event:MouseEvent):void
{
trace("Mouse clicked");
}
I also have the simple tween of rotation already created within the instance of the pipe, but dunno how to connect it to code.
I'm supposed to figure out what to put inside the function but I honestly have no clue. Hours of googling have come up with nothing either except a 12 dollar purchasable source code for an even more complicated pipe game.
I hope somebody can at least help a bit, and thanks.
The way to rotate a clip is via it's rotation property. It defaults to 0.
If you were set the rotation property of your tile to 90, you'd rotate your pipe tile 90 degrees.
for example :
pipe_1.rotation += 90;
A tween is a means of changing a property of a given DisplayObject over time. So what you want to do is tween your rotation property 90 degrees over time.
Here is a tutorial on Tweening - http://www.republicofcode.com/tutorials/flash/as3tweenclass/
I think it'd be more beneficial for you to take the time to learn about it, than to have me just write a few lines of code to solve your problem.
StackOverflow is a place where you can ask a question, AFTER you have tried something and have hit an issue.
I have provided you with the basic concept of what you need to do, and if you take the time to learn about tweening, you'll be able to achieve your goal rather simply.
There are also tweening libraries such as TweenLite and TweenMax that simplify tweening. Not sure if your class will allow you to use them, but worthwhile to check out for your own benefit.
You can find TweenLite here :
http://www.greensock.com/tweenlite/
Are you talking about a frame by frame tween? or tweening with code?
for frame by frame tweening, you can try to do this:
pipe_1.addEventListener(MouseEvent.CLICK, f1_MouseClickHandler);
function f1_MouseClickHandler(e:MouseEvent) {
pipe_1.gotoAndPlay(2); //if the tween starts at frame 2
}
For code tweening, just call the tween function inside that handler function

hitTestPoint with 'shapeFlag=true' doesn't work in AS3

I simply added a sprite in AS3:
Sprite myspr = new Sprite();
myspr.addChild(mybitmap);
addChild(myspr);
Then I added an event. I did hitTestPoint for checking mouse is over my sprite or not.
stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseCheck);
private function mouseCheck(evt:MouseEvent):void {
var xx:int = stage.mouseX;
var yy:int = stage.mouseY;
if(myspr.hitTestPoint(xx, yy, true)) {
...
// I'm checking mouse over here.
}
evt.updateAfterEvent();
}
Problem is: hitTestPoint gives true when mouse comes to full boundary box. But it should give true only if mouse comes on transparent isometric sprite.
Is there a solution for this, thanks in advance.
this should help. You need pixel perfect detection.
Actionscript 3 pixel perfect collision. How to? (learning purposes)
http://www.freeactionscript.com/2011/08/as3-pixel-perfect-collision-detection/
http://www.anotherearlymorning.com/2009/07/pixel-perfect-collision-detection-in-actionscript-3/
http://old.troygilbert.com/2009/08/pixel-perfect-collision-detection-revisited/
There's a few ways I usually do hit testing.
1) The easiest way is to use a an already made class that you can find online. Some people much smarter than me have created complex classes that allow for much better pixel to pixel interaction. The ones listed by Paras are all good. The problem with these is, for newer users, it can be hard to understand all the code and how to implement them. Usually it is simple once you understand what is going on though. You just replace your hit test with the class file and then enter in the correct arguments.
2) Another method is to actually go into the symbol, create a new layer, and then draw a rectangle(just turn the alpha down to 0%) where you want the hit test to work. This may seem like a stupid method, after all we are just confined to a square once again. BUT, it will actually work MUCH better than you'd expect. Just draw the square maybe slightly smaller than the height and width of your character you're detecting the hit test on, and you should be good to go. Give it an instance name (the hit square that is) and then just perform the hitTest with that square instead of the actual sprite. It works wonderfully and is a very simple solution. For what you're explaining though, this sounds like it might not work. This method is more from a gamer standpoint. It looks good when attacking and getting hit by enemies, but isn't necessarily exact. Also, if you want to do this with two characters (maybe a large attack hitting an enemy) simply draw a hit box for both sprites. This is probably a little more basic than using a pre-made pixel perfect hit detection test, but it works extremely well and takes only a few minutes.

Sound plays multiple times at once

I'm having trouble with sound in Flash. I may have went about coding the wrong way, because most of my codes are on frames.
So, I have these two variables
var outsideDay:Sound = new daysong();
var outsideNight:Sound = new nightsong();
And I want to play these songs on a specific frame. However, the sounds play sporadically, like 50 times at once. I think it's because I have other codes that link to the frames with a Enter_Frame function. How can I get the sounds to loop and not play multiple times at once?
Have you dropped the sound anywhere on the timeline in any frame? If so remove that frame.
Also, if you have your code declared on a keyframe that does not have a stop(); call on it, likely it is hitting that frame over and over again, when it "enters" it. Try adding stop(); either at the beginning of your code or at an ending key-frame, wherever it makes most sense for your project.
After trying those two things, another method I have learned to love that may come in handy is:
flash.media.SoundMixer.stopAll();
This will stop all sounds so that whatever sounds you start to play after making this call will not have other previously started sounds to contend with.
This sound tutorial may also be of use to you.
Let us know how it goes, or if any of this stuff helped.