Some Collisions Aren't Working (Actionscript 3) - actionscript-3

I'm new to Actionscript and I'm playing around and testing with collisions. I have it working okay, but there are platforms where the player can just jump straight up and through and sometimes can walk straight though when coming from the right.
Here's my .fla file so you can test it and see the code:
http://www.mediafire.com/?sspz4grjilvvdd6
I think it's because of the I've used points. How can I fix this?
Also, is it true using hitTestObject can sometimes detect collisions when there isn't actually a collision. That's why I've tried points.
Thanks for the help!

Try this function. All you'll need is to put your objects inside movieclips and name them.

Related

Flash Pro: Collision of objects only works correctly when on Frame 1

as you may have noticed just by reading the title it is quite hard to summarize the problem,
which is probably why I couldn't find a solution anywhere.
I am currently working on a flash game using adobe flash pro CC.
I started by creating the first level of my sidescrolling game and got everything to work just as I wanted it to. All my work was on a single frame (frame 1) and on a single layer and it worked fine.
However, when I then went on to move the frame a little further back and added frames in front of it for the menu I encountered a problem.
When I "gotoAndStop" to the frame with my game content on it, everything works except the collision between my character and my objects. My character is now sunk into the "floor object" about have his size, while when i move the frame back to "frame 1" everything works fine again.
I am losing my mind over this. Please help me :(
in flash when you start changing a dynamic property such as x or y, that object will be independent from the keyframe where it originally was ~forever~, even if you go back to the original keyframe. therefore if you start some enterframe and change the playhead afterwards you will get some awkward (but expected) behaviour.
ideally, jump to the game keyframe and just then call whatever methods needed to start the game.

Need a hitTestPoint solution to replace hitTestObject

I need an as3 solution to this collision problem:
I have this code:
else if (objectArray[i].toString().indexOf("meandude") != -1) {
//if the object is a meandude and hits it, game over
if (projectileMC.hitTestObject(objectArray[i])) {
removeEventListener(Event.ENTER_FRAME,mainEnterFrame);loseGame();
which is using the bounding boxes of the projectileMC and meandude (hitTestObject). I'd like it so the center points of the projectileMC and meandude crossing registers the action instead (hitTestPoint). I tried to switch it myself, but I'm not getting it to work.
Thanks
Not clear what your precise problem is without seeing more of your code. But as a first guess I would think you want to solve the following problem:
Your hittest is not being triggered when your projectile hits the "meandude" when you use the hitTestObject.
And
You try to solve the problem by using the hitTestPoint instead?
The test might not trigger simple because your projectile is moving too fast. If the distance between XY_ThisFram before colisison and then XY_NextFrame is bigger than the size of of your "meandude" it won't ever collide. I suggest you don't use hitTestObject to do any collision in your game as it is not reliable, especially for fast moving display objects.
Flash doesn't have a good object-to-object hit test for the general case. If both projectileMC and meandude are bitmaps, you can try BitmapData.hitTest.

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.

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.

AS3 Play contents of a symbol

I've grouped a multiple layer 1 frame actionscript driven animation into a symbol. (simply put, imagine a 1 frame image being tweened by code over a few sec, in this symbol)
Now I'd like to play the contents of this symbol using an event listener.
But
stuff_mc.gotoAndPlay(1); and Test Movie doesn't play a thing (doesn't even show the contents of the symbol (the picture) which can actually be seen on stage).
If I go inside the stuff_mc and Test Scene, it works perfectly..
So how do I play stuff_mc from the stage?
Thanks in advance
It sounds to me like you are using multiple scenes. If that's the case then this is most likely where your problem is. Scenes are generally a bad idea in Flash – Adobe itself admits this.