Player freezes after touching wall in maze game and won't move - actionscript-3

I'm trying to code so you can't walk thorugh the wall, so far the player does stop if you touch the wall, but won't move after touching it, it's completely stuck.
Can you see what the problem is with the code?
if(rightPressed)
{
if(playerMc.x<800)
{
if(overworldMc.hitTestPoint(playerMc.x+15,playerMc.y,true))
{
playerMc.x += playerSpeed; playerSpeed= 0;
}
else
{
playerMc.y += playerSpeed; playerSpeed= 5;
}
}
}

Related

AS3 - Endless Vertically Scrolling Background

As my first AS3 project, I'm developing a simple space shooter/bullet hell game. I looked online for a way of producing an infinite background scrolling effect and found this working code.
public function backgroundScroll (evt:Event):void
{
background1.y -= scrollspeed;
background2.y -= scrollspeed;
if (background1.y <= -background2.height)
{
background1.y = background2.y + background2.height;
}
else if (background2.y <= -background2.height)
{
background2.y = background1.y + background1.height;
}
}
This causes the background to infinitely scroll upwards from the bottom of the screen without fail, but no matter what I try, I cannot cause it to instead infinitely scroll down from the top of the screen. Any help would be greatly appreciated.
Code seems to be works fine, i think you have some trubles with names and with events.
How is code working :
enter image description here
Make sure that :
1) You have add ENTER_FRAME listener
this.addEventListener(Event.ENTER_FRAME, backgroundScroll );
2) background1 & background1 is names of objects, that you need
I assume you have two backgrounds that are larger than the height of your stage (so they would switch when the are going off-screen at the bottom. Note that the "- background1.height" parts have changed as well
// at the beginning you set the backgrounds one below another
background1.y = 0;
background2.y = background1.height;
public function backgroundScroll (evt:Event):void
{
background1.y += scrollspeed;
background2.y += scrollspeed;
if (background1.y >= stage.stageHeight)
{
// the background is below the visible stage area, put it above the other background
background1.y = background2.y - background1.height;
}
else if (background2.y >= stage.stageHeight)
{
background2.y = background1.y - background2.height;
}
}

AS3 Sprite not going behind object

I'm trying to make a pseudo 3D collision with multiple walls. It works to an extent:
I can collide with the sides of each wall
but only on one of the walls, if I were to go to the top, It goes behind the rectangle like this but on the second rectangle, I go above it like this I don't why this is happening (I am a beginner in AS3). Using trace, it says that the player is at layer 0 while the wall is at layer 2, but the ball still goes above the wall.
edit: I'm using flashdevelop incase that changes anything
Edit: Changed code, it works fine, but ignores the second wall, it prints it's name but doesn't change the boolean
for each (var wall in WallsList)
{
trace(wall.name)
if (wall.hitTestPoint(Character.x - hitRad+.995, Character.y, true)) //col right
{
Character.x+=CharacterSpeed;
}
if (wall.hitTestPoint(Character.x + hitRad-.995, Character.y, true)) //col left
{
Character.x-=CharacterSpeed;
}
if (wall.hitTestPoint(Character.x , Character.y- hitRad+25, true)) //col bottom
{
Character.y+=CharacterSpeed;
}
if (wall.hitTestPoint(Character.x, Character.y+hitRad-25, true)) //col top
{
Character.y -= CharacterSpeed;
}
if (wall.hitTestPoint(Character.x, Character.y+hitRad, true)) // col top #2
{
trace(wall.name) // prints for both walls
Top = true; // only changes for 1 wall
}
if (!wall.hitTestPoint(Character.x, Character.y+hitRad, true))
{
Top = false;
}
}
Bring wall object above the ball:
setChildIndex(wallObject, this.numChildren - 1);

flash as3 hitdetection function triggering other function

I am trying to code a script in which a movieclip drops a rope and catches fishes that follows it up if it touches it. here is the issue , i am using hitTestObject to detect collision . Ofcourse the problem is that i trigger the function when it touches but as soon as it doesnt touch the function for moving the movie starts so basically the fish goes up for few seconds and then starts moving straight again .
To try and fix that i tried to make a boolean variable which changes to true or false according to hit and accordingly makes the movieclip moves but also doesnt work because as soon as one mc is not touching the other it changes from true to false or 1 to 0 ..tried both (as in with boolean variable and Number variable) . Any help or putting me on the right direction would be highly appreciated . Thank you so much
// fish capture code
this.addEventListener(Event.ENTER_FRAME,handleCollision);
function handleCollision(e:Event):void
{
if (ropeHit.hitTestObject(fishy_1_a))
{
stopFish1();
trace(movefish1);
}
else
{
moveFish1();
}
}
//code enemy fishy
//fish 1 A
function moveFish1()
{
if (fishy_1_a.x < 800)
{
fishy_1_a.x += xSpeed;
}
else if (fishy_1_a.x >= 800)
{
fishy_1_a.x = -100;
}
}
function stopFish1()
{
fishy_1_a.y -= xSpeed;
}
Define some flag, that you can test:
function handleCollision(e:Event):void {
//Check if fishy is caught
if (!fishy_1_a.catched && ropeHit.hitTestObject(fishy_1_a)) {
//Change flag
fishy_1_a.catched = true;
trace("Gotcha!");
}
if (fishy_1_a.catched) {
stopFish1();
}else {
moveFish1();
}
}

Animated presentation navigation by arrows - AS3

I'm new to AS3 and although I was looking for a solution to my problem truly long time, I was not successful. I have an animated presentation and I just want to make navigation through that by arrows. Everything is on the main timeline. In the first frame I made this code
var zpet: Number;
if (currentFrame == 1) {
zpet = 1;
}
stage.addEventListener(KeyboardEvent.KEY_DOWN, posun);
function posun(event: KeyboardEvent): void {
if (event.keyCode == 37) {
addEventListener(Event.ENTER_FRAME, playReverse);
function playReverse(event: Event): void {
if (currentFrame == zpet) {
stopPlayReverse();
} else {
prevFrame();
}
}
function stopPlayReverse(): void {
if (hasEventListener(Event.ENTER_FRAME)) {
removeEventListener(Event.ENTER_FRAME, playReverse);
}
}
} else if (event.keyCode == 39) {
if (currentFrame < totalFrames - 1) {
play();
} else {
stop();
}
}
}
stop();
Moving forward works perfect as I put stop(); in every keyframe of the presentation. The problem is how to go back just to the previous keyframe (and I also want to go back in reverse playing). I thought it would be quite easy if I made a variable (called "zpet") and set it the specific number of frame where to go back in each keyframe. But it doesn't work, all the time it's going back to frame 1. For example I put in the frame 26 the code zpet = 13; that should say when playing back from the frame 26 stop at the frame 13. Any ideas how to solve this? I would be really grateful for that..
You can label each keyframe of your animation anything you want directly from the timeline and then something like this :
function playReverse(event: Event): void
{
prevFrame();// You can also use gotoAndStop(currentFrame - 1)
if(currentFrameLabel != null)
stopPlayReverse();
}
Looks cleaner imo, plus you can use labels value later in case you make a scene selection menu.

Load objects which contain webcam-video out of the library with ActionScript 3.0

I want to create a simple balloon shooter with Actionscript. The balloons should float from the bottom to the top end of the stage. There will be two kinds of balloons. One will carry a frame, where the image of the webcam is attached.
If you shoot the one without the image, you get 100 points. If you shoot the one with the image, you lose.
My solution so far addresses every single element (graphics as well as the video) individually. This would mean, that I have to write that code for a couple of times for every ballon that should rise. This seems not very efficient. Also, the amount of balloons is limited.
import flash.media.Camera;
import flash.media.Video;
import flash.display.MovieClip;
var camera:Camera = Camera.getCamera();
if (camera != null)
{
videoFrame1.attachCamera(camera);
}
else
{
trace("You need a camera.");
}
stop();
Score.appendText("Your Score: ");
//Balloon1
var directionY1;
video1.addEventListener(Event.ENTER_FRAME, float1);
function float1(event:Event)
{
if (video1.y > 0)
{
richtungY1 = 1;
}
video1.y -= 3 * directionY1;
}
videoFrame1.addEventListener(Event.ENTER_FRAME, Framefloat1);
function Framefloat1(event:Event)
{
if (videoFrame1.y > 0)
{
directionY1 = 1;
}
videoFrame1.y -= 3 * directionY1;
}
clickFrame1.addEventListener(Event.ENTER_FRAME, clickFramefloat1);
function clickFramefloat1(event:Event)
{
if (clickFrame1.y > 0)
{
directionY1 = 1;
}
clickFrame1.y -= 3 * directionY1;
}
balloon1.addEventListener(Event.ENTER_FRAME, Balloonfloat1);
function Balloonfloat1(event:Event)
{
if (balloon1.y > 0)
{
directionY1 = 1;
}
balloon1.y -= 3 * directionY1;
}
tv1.addEventListener(Event.ENTER_FRAME, TVfloat1);
function TVfloat1(event:Event)
{
if (tv1.y > 0)
{
directionY1 = 1;
}
tv1.y -= 3 * directionY1;
}
function clicked(event:MouseEvent):void
{
removeChild(videoFrame1);
removeChild(tv1);
removeChild(balloon1);
}
clickFrame1.addEventListener(MouseEvent.CLICK, clicked);
//Counter;
clickFrame1.addEventListener(MouseEvent.CLICK, count);
var counter = 0;
function count( e:MouseEvent ):void
{
counter += 100;
Score.appendText(counter);
trace( counter );
}
Now, my idea was to build one kind of each balloon, and load it out of the library. I thought about using addChild. If you shoot one balloon, a new one should be generated, with increased speed. But I couldn't work it out.
It seems I can't put the video inside a movieClip and combine it with the other elements to one object.
Does anyone have an idea, how I could create a movieClip, with all the elements inside, and load it out of the library?
Thank you very much in advance!
This tutorial explains how to load assets from the library to the timeline: http://www.adobe.com/devnet/flash/quickstart/loading_images_library_as3.html. It will get you started.