Fishing Game Code ActionScript 2.0 to ActionScript 3.0 - actionscript-3

I am trying to make a fishing game where each fish you catch gives you a point and different enemies subtract points. I have found a tutorial for this in ActionScript 2 but I can't find any for action script 3 (here is the link to what I want to replicate: http://www.flashkit.com/tutorials/Games/Creating-Imtiazur-1297/index.php). I am relatively new at action script as I have only made animations before but I was able to do a little but then couldn't figure out the rest.
I currently have the fishing line moving up and down and does not go beyond the fishing line but I'm not sure how to make the line get longer when it goes down. At the moment the whole line and hook move with a space between it and the rod. I also have to click before the line moves (I'm not sure how to make it instantly usable when going to the frame). I also have one fish moving across the screen. I don't know how to make multiple of the same fish or enemies going across the screen at the same time.
Any help would be greatly appreciated​!
fish.addEventListener(Event.ENTER_FRAME, fl_AnimateHorizontally);
function fl_AnimateHorizontally(event:Event)
{
fish.x = fish.x + 5; if (fish.x>1100)
{
fish.x = -100;
}
shark.x = shark.x + 7; if (shark.x>3500)
{
shark.x = -400;
}
jellyfish.x = jellyfish.x + 3; if (jellyfish.x>3000)
{
jellyfish.x = -100;
}
boot.x = boot.x + 5; if (boot.x>1500)
{
boot.x = -100;
}
barrel.x = barrel.x + 4; if (barrel.x>1600)
{
barrel.x = -100;
}
}
var upPressed:Boolean = false;
var downPressed:Boolean = false;
var leftPressed:Boolean = false;
var rightPressed:Boolean = false;
rope.addEventListener(Event.ENTER_FRAME, fl_MoveInDirectionOfKey);
stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_SetKeyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, fl_UnsetKeyPressed);
function fl_MoveInDirectionOfKey(event:Event)
{
if (upPressed)
{
rope.y -= 10; hook.y -= 10;
}
if (downPressed)
{
rope.y += 10; hook.y += 10;
}
if (leftPressed)
{
rope.x -= 0; hook.x -= 0;
}
if (rightPressed)
{
rope.x += 0; hook.x += 0;
}
if (rope.y < 35)
{
rope.y = 35; hook.y = 100;
}
}
function fl_SetKeyPressed(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.UP:
{
upPressed = true;
break;
}
case Keyboard.DOWN:
{
downPressed = true;
break;
}
case Keyboard.LEFT:
{
leftPressed = true;
break;
}
case Keyboard.RIGHT:
{
rightPressed = true;
break;
}
}
}
function fl_UnsetKeyPressed(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.UP:
{
upPressed = false;
break;
}
case Keyboard.DOWN:
{
downPressed = false;
break;
}
case Keyboard.LEFT:
{
leftPressed = false;
break;
}
case Keyboard.RIGHT:
{
rightPressed = false;
break;
}
}
}

Related

Moving running cycle in AS3

I want to make a moving character that runs a movie clip when a keyboard arrow is pressed.For example:
When no arrows are pressed I want the character not to move when any keyboard arrows are pressed and run a movie clip animation where the character runs when the right arrow is pressed and same with the left arrow.
var upPressed:Boolean = false;
var downPressed:Boolean = false;
var leftPressed:Boolean = false;
var rightPressed:Boolean = false;
movieClip_1.addEventListener(Event.ENTER_FRAME,fl_MoveInDirectionOfKey);
stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_SetKeyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, fl_UnsetKeyPressed);
function fl_MoveInDirectionOfKey(event:Event)
{
if (upPressed)
{
movieClip_1.y -= 5;
}
if (downPressed)
{
movieClip_1.y += 5;
}
if (leftPressed)
{
movieClip_1.x -= 5;
}
if (rightPressed)
{
movieClip_1.x += 5;
}
}
function fl_SetKeyPressed(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.UP:
{
upPressed = true;
break;
}
case Keyboard.DOWN:
{
downPressed = true;
break;
}
case Keyboard.LEFT:
{
leftPressed = true;
break;
}
case Keyboard.RIGHT:
{
rightPressed = true;
break;
}
}
}
function fl_UnsetKeyPressed(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.UP:
{
upPressed = false;
break;
}
case Keyboard.DOWN:
{
downPressed = false;
break;
}
case Keyboard.LEFT:
{
leftPressed = false;
break;
}
case Keyboard.RIGHT:
{
rightPressed = false;
break;
}
}
}
My movie clips are: run_right and run_left.
My timeline:
Create a MovieClip called "movieClip_1".
var movieClip_1= new MovieClip_1();
Inside this MovieClip, add "run_right" animation on the first frame and "run_left" on the second frame.
I mean, add new MovieClips that contains your animations.
Then, go to fl_MoveInDirectionOfKey function and write this:
if (rightPressed)
{
movieClip_1.gotoAndStop(1);
}
else if (leftPressed)
{
movieClip_1.gotoAndStop(2);
}
else
{
// no animation
// movieClip_1.gotoAndStop(3); idle animation could be on frame 3
}
here is your Mistake
movieClip_1.addEventListener(Event.ENTER_FRAME,fl_MoveInDirectionOfKey);
------------
remove
movieClip_1.
it should be like this
stage.addEventListener(Event.ENTER_FRAME,fl_MoveInDirectionOfKey);
This is second solution, answer ( the Event.Enter_Frame ) is inside movieClip so you need to add (root.) before MovieClip
change it like this
if (upPressed)
{
root.movieClip_1.y -= 5;
}
if (downPressed)
{
root.movieClip_1.y += 5;
}
if (leftPressed)
{
root.movieClip_1.x -= 5;
}
if (rightPressed)
{
root.movieClip_1.x += 5;
}

How to move object in the direction of rotation in flash?

I have a "car" which has to be moved in the direction which it is rotated.
Now it just rotates in a direction and keeps on going up and down.Please help me.
I am using adobe flash pro cs6 and actionscript3.
My code is :
var upPressed:Boolean = false;
var downPressed:Boolean = false;
var leftPressed:Boolean = false;
var rightPressed:Boolean = false;
car.addEventListener(Event.ENTER_FRAME, fl_MoveInDirectionOfKey);
stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_SetKeyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, fl_UnsetKeyPressed);
function fl_MoveInDirectionOfKey(event:Event)
{
if (upPressed)
{
car.y += 5;
}
if (downPressed)
{
car.y -= 5;
}
if (rightPressed)
{
car.rotation += 5;
}
if (leftPressed)
{
car.rotation -= 5;
}
}
function fl_SetKeyPressed(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.UP:
{
upPressed = true;
break;
}
case Keyboard.DOWN:
{
downPressed = true;
break;
}
case Keyboard.LEFT:
{
leftPressed = true;
break;
}
case Keyboard.RIGHT:
{
rightPressed = true;
break;
}
}
}
function fl_UnsetKeyPressed(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.UP:
{
upPressed = false;
break;
}
case Keyboard.DOWN:
{
downPressed = false;
break;
}
case Keyboard.LEFT:
{
leftPressed = false;
break;
}
case Keyboard.RIGHT:
{
rightPressed = false;
break;
}
}
}
You need to use vector math to move the car's x,y based on an angle and a distance.
For example, you could move your car based on an angle and speed like this:
function move(degrees:Number, speed:Number):void {
var radians:Number = degrees * (Math.PI / 180);
car.x += Math.cos(radians) * speed;
car.y += Math.sin(radians) * speed;
}
Then you can use the car's rotation as the angle and 5 or -5 as the speed:
if (upPressed) {
move(car.rotation, 5);
}
if (downPressed) {
move(car.rotation, -5);
}
Note that this assumes rotation=0 means your car is facing right-ward. If you've draw your car facing a different direction you'll need to compensate for the angle you've drawn the car, for example if the car is facing upward you need to use move(car.rotation - 90, 5).

Arrow keys not detected once the SWF file is published

I'm using flash CS4 to have an animated character moving within the stage boundaries. There are no code errors when generating the SWF file and the character is moving fine when I test the movie in Flash. Once I publish the file, the character is completely unresponsive to the arrow keys. I have no clue as what the issue might be and some help would be greatly appreciated. Here's the code of the movie :
stop();
var upPressed:Boolean = false;
var downPressed:Boolean = false;
var leftPressed:Boolean = false;
var rightPressed:Boolean = false;
var isWalking:Boolean = false;
var mySpeed:Number = 3;
my_Sprite.addEventListener(Event.ENTER_FRAME, moveSprite);
stage.addEventListener(KeyboardEvent.KEY_DOWN, setKeyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, unsetKeyPressed);
function moveSprite(event:Event):void {
if(upPressed && my_Sprite.y >= 40){
my_Sprite.y -= mySpeed;
}
if(downPressed && my_Sprite.y <= 440){
my_Sprite.y += mySpeed;
}
if(leftPressed && my_Sprite.x >= 20){
my_Sprite.x -= mySpeed;
}
if(rightPressed && my_Sprite.x <= 600){
my_Sprite.x += mySpeed;
}
}
function setKeyPressed(event:KeyboardEvent):void {
switch(event.keyCode){
case Keyboard.UP: {
upPressed = true;
if (!isWalking) {
my_Sprite.gotoAndPlay("Up");
isWalking = true;
}
break;
}
case Keyboard.DOWN: {
downPressed = true;
if (!isWalking) {
my_Sprite.gotoAndPlay("Down");
isWalking = true;
}
break;
}
case Keyboard.LEFT: {
leftPressed = true;
if (!isWalking) {
my_Sprite.gotoAndPlay("Left");
isWalking = true;
}
break;
}
case Keyboard.RIGHT: {
rightPressed = true;
if (!isWalking) {
my_Sprite.gotoAndPlay("Right");
isWalking = true;
}
break;
}
}
}
function unsetKeyPressed(event:KeyboardEvent):void {
switch(event.keyCode) {
case Keyboard.UP: {
upPressed = false;
my_Sprite.gotoAndStop("Up");
isWalking = false;
break;
}
case Keyboard.DOWN: {
downPressed = false;
my_Sprite.gotoAndStop("Down");
isWalking = false;
break;
}
case Keyboard.LEFT: {
leftPressed = false;
my_Sprite.gotoAndStop("Left");
isWalking = false;
break;
}
case Keyboard.RIGHT: {
rightPressed = false;
my_Sprite.gotoAndStop("Right");
isWalking = false;
break;
}
}
}
You may need to add some JavaScript to your HTML to set the focus on the Flash object:
<body onLoad="window.document.movieID.focus();">
Make sure you change movieID to whatever you actually set as your ID.
you can force the focus by doing
this.stage.focus = myTextField; // assuming this is added to the display hierarchy

Flash cc as3 how to make a child object be effected the same way as the parent

I have generated a piece of code, with help of this message board, but it is one problem after another. This time I have it so when you click on an object a new object spawns, but I need it so the child (the one that has been spawned) is effected by the gravity physics in place.
Here is my code:
var drag:Number = 1;
var BlockAppear = new MovieClip;
gotoAndStop(1, "Minigame-Block");
HeartBear.addEventListener(MouseEvent.MOUSE_DOWN, fl_ClickToDrag_4);
import flash.events.MouseEvent;
HBS.addEventListener(MouseEvent.CLICK, addHeart);
function addHeart(e:MouseEvent):void
{
var H=new HB;
H.y = 523;
H.x = 721;
addChild(H); //need the child to be effected by the gravity physics.
}
Block.addEventListener(Event.ENTER_FRAME, fl_MoveInDirectionOfKey_3);
stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_SetKeyPressed_3);
stage.addEventListener(KeyboardEvent.KEY_UP, fl_UnsetKeyPressed_3);
function fl_MoveInDirectionOfKey_3(event:Event)
{
if (leftPressed)
{
Blockr.x -= 5;
}
if (rightPressed)
{
Block.x += 5;
}
}
function fl_SetKeyPressed_3(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.LEFT:
{
leftPressed = true;
break;
}
case Keyboard.RIGHT:
{
rightPressed = true;
break;
}
}
}
function fl_UnsetKeyPressed_3(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.LEFT:
{
leftPressed = false;
break;
}
case Keyboard.RIGHT:
{
rightPressed = false;
break;
}
}
}
function fl_ClickToDrag_4(event:MouseEvent):void
{
HeartBear.startDrag()
gravity = 0
}
stage.addEventListener(MouseEvent.MOUSE_UP, fl_ReleaseToDrop_4);
function fl_ReleaseToDrop_4(event:MouseEvent):void
{
HeartBear.stopDrag();
gravity = 0.9
}
var gravity = 0.8;
var floor = 523.3;
var onFloor:Boolean = false;
HeartBear.y = floor;
HeartBear.speedY = 0;
HeartBear.jump = 15;
stage.addEventListener(Event.ENTER_FRAME, enterframe);
function enterframe(e:Event) {
HeartBear.speedY += gravity;
HeartBear.y += HeartBear.speedY;
if(HeartBear.y > floor) {
HeartBear.speedY = 0;
HeartBear.y = floor;
onFloor = true; //gravity physics
}
}
stage.addEventListener(KeyboardEvent.KEY_DOWN, space);
function space(e:KeyboardEvent) {
if(e.keyCode == Keyboard.UP && onFloor) {
HeartBear.speedY = -HeartBear.jump;
onFloor = false;
}
}
Shopclicker.addEventListener(MouseEvent.CLICK, fl_ClickToGoToScene_3);
function fl_ClickToGoToScene_3(event:MouseEvent):void
{
MovieClip(this.root).gotoAndStop(1, "Shop");
}
Change
function enterframe(e:Event) {
HeartBear.speedY += gravity;
HeartBear.y += HeartBear.speedY;
if(HeartBear.y > floor) {
HeartBear.speedY = 0;
HeartBear.y = floor;
onFloor = true; //gravity physics
}
}
to
function enterframe(e:Event) {
e.currentTarget.speedY += gravity;
e.currentTarget.y += e.currentTarget.speedY;
if(e.currentTarget.y > floor) {
e.currentTarget.speedY = 0;
e.currentTarget.y = floor;
onFloor = true; //gravity physics
}
}
and change
stage.addEventListener(Event.ENTER_FRAME, enter frame);
to
HeartBear.addEventListener(Event.ENTER_FRAME, enterframe);
and change
function addHeart(e:MouseEvent):void
{
var H=new HB;
H.y = 523;
H.x = 721;
addChild(H); //need the child to be effected by the gravity physics.
}
to
function addHeart(e:MouseEvent):void
{
var H=new HB;
H.y = 523;
H.x = 721;
addChild(H); //need the child to be effected by the gravity physics.
H.addEventListener(Event.ENTER_FRAME, enterframe);
}
And try to camel-case your variable and function names as well as class names.

Game Rotation Collision Problems

Im attempting to make a basic maze game with a slight twist. At set intervals (set by a timer event) the maze rotates 90%. The problem I'm having is in regards to the hitTestPoint. The hit test works prior to the maze roataion and works after a full 360% rotation but stops working during the 90%, 180% and 270% rotation points. I have exhausted all my knowledge (As limited as a 5 month AS3 programmer) in AS3 to resolve this problem and am at my wits end.
The maze is in a container and the container is rotating, this is so that the maze in effect has a secondary moving pivot point that continually follows the player around tha maze. additionally the container is moving on key press not the player.
Could anyone please halp me by explaining what is causing the problem, how I can fix it and if possible show me an example of the code I should be using.
Here is what I have so far.
stop();
import flash.events.Event;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.sensors.Accelerometer;
var speed:Number = 5;
var northSpeed = speed;
var southSpeed = speed;
var eastSpeed = speed;
var westSpeed = speed;
var upPressed:Boolean = false;
var downPressed:Boolean = false;
var leftPressed:Boolean = false;
var rightPressed:Boolean = false;
var Orientation:int = 0;
var count:int = 0;
var timer:Timer = new Timer(1000,60);
var time = 60;
player.addEventListener(Event.ENTER_FRAME, MovePlayer);
containBox.maze.addEventListener(Event.ENTER_FRAME, hitWalls);
stage.addEventListener(KeyboardEvent.KEY_DOWN, KeyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, KeyDepressed);
containBox.maze.addEventListener(Event.ENTER_FRAME, spin);
timer.start();
timer.addEventListener(TimerEvent.TIMER, timerHandle);
function timerHandle(e:TimerEvent):void
{
txt_time.text = time;
time--;
}
function hitWalls(event:Event):void
{
if (upPressed==true && containBox.maze.hitTestPoint(player.x,player.y,true))
{
northSpeed = 0;
player.y = player.y+=2;
}
else
{
northSpeed = speed;
}
}
function spin(event:Event):void
{
if (time <= 0)
{
txt_time.text = "TIMES UP!";
}
if (time <= 54)
{
containBox.rotation = 90;
Orientation = 1;
}
if (time <= 50)
{
containBox.rotation = 180;
Orientation = 2;
}
if (time <= 48)
{
containBox.rotation = 270;
Orientation = 3;
}
if (time <= 46)
{
containBox.rotation = 0;
Orientation = 0;
}
}
function MovePlayer(event:Event):void
{
if (Orientation == 0)
{
if (upPressed)
{
containBox.maze.y += northSpeed;
}
if (downPressed)
{
containBox.maze.y -= southSpeed;
}
if (leftPressed)
{
containBox.maze.x += westSpeed;
}
if (rightPressed)
{
containBox.maze.x -= eastSpeed;
}
}
else if (Orientation == 1)
{
if (upPressed)
{
containBox.maze.x += 5;
}
if (downPressed)
{
containBox.maze.x -= 5;
}
if (leftPressed)
{
containBox.maze.y -= 5;
}
if (rightPressed)
{
containBox.maze.y += 5;
}
}
else if (Orientation == 2)
{
if (upPressed)
{
containBox.maze.y -= 5;
}
if (downPressed)
{
containBox.maze.y += 5;
}
if (leftPressed)
{
containBox.maze.x -= 5;
}
if (rightPressed)
{
containBox.maze.x += 5;
}
}
else if (Orientation == 3)
{
if (upPressed)
{
containBox.maze.y += 5;
}
if (downPressed)
{
containBox.maze.y -= 5;
}
if (leftPressed)
{
containBox.maze.x += 5;
}
if (rightPressed)
{
containBox.maze.x -= 5;
}
}
}
function KeyPressed(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.UP :
{
upPressed = true;
break;
};
case Keyboard.DOWN :
{
downPressed = true;
break;
};
case Keyboard.LEFT :
{
leftPressed = true;
break;
};
case Keyboard.RIGHT :
{
rightPressed = true;
break;
}
}
}
function KeyDepressed(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.UP :
{
upPressed = false;
break;
};
case Keyboard.DOWN :
{
downPressed = false;
break;
};
case Keyboard.LEFT :
{
leftPressed = false;
break;
};
case Keyboard.RIGHT :
{
rightPressed = false;
break;
}
}
}
Many thanks,
Reece.
You need to do 2D coordinate system transformation to do a proper comparison. See, the player basically stands on a fixed point, thus (player.x, player.y) is fixed, and hitTestPoint() uses the coordinates supplied as local to the this object, in your case the maze. To get the player's coordinates in the maze's system, you need first get global position, then derive local position, there are functions for that, localToGlobal() and globalToLocal() respectively.
function hitWalls(event:Event):void
{
var p:Point=new Point(player.x,player.y);
var dp:Point=containBox.maze.globalToLocal(player.parent.localToGlobal(p));
// now test vs dp instead of player.
if (upPressed==true && containBox.maze.hitTestPoint(dp.x,dp.y,true))
{
northSpeed = 0;
player.y = player.y+=2;
}
else
{
northSpeed = speed;
}
}