im trying to make a teleporter game and my character needs to have some velocity and gravity, does anyone know what sums i need to be able to acomplish this?
This is my code so far:
var char = this.addChild(new Char());
char.width = 20;
char.height = 20;
char.x = startPos.x; //startPos is an invisible movieclip that I can move around to make the starting position
char.y = startPos.y; // accurate
var vx:Number = 0;
var vt:Number = 0;
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
function keyDownHandler (e:KeyboardEvent):void {
switch (e.keyCode) {
case Keyboard.UP:
char.y = char.y - 5
}
}
If your char only needs to go up then the following code will do the job.
But if it needs to move in all direction then much advanced code is required.
Follow Moving Character in all directions.
This is a quick solution to your need.
var gravity:Number = 2;
var velocity:Number = 1.1;
var move:Boolean = false;
function moveChar(e:Event):void
{
if(move)
{
gravity *= velocity;
char.y -= gravity; // move char
}
}
char.addEventListener(Event.ENTER_FRAME, moveChar, false, 0, true);
//Keyboard events
function keyDownHandler (e:KeyboardEvent):void
{
switch (e.keyCode)
{
case Keyboard.UP:
move = true;
}
}
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
function keyUpHandler (e:KeyboardEvent):void
{
move = false;
gravity = 2;
}
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
Related
Wondering if you guys can help me. I've used online tutorials to create the following.
I am working on a small flash project and I've hit a wall. Basically, I've got 3 scenes. A home page, and 2 games. My home page and one of the games are programmed on the timeline within an actions layer. The third game is applied to a Main.as. The problem is that I want to apply a button called home to the Flappy game, but since I've used the .as file for this code, I'm unsure how to do it.
Basically, my question is "How do I add a button to the "Flappy" Scene? Each time i try to do it and run a test, It's playing the Flappy Game over the Scene 1.
Main.as ("Flappy" Scene3)
package{
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.events.Event; //used for ENTER_FRAME event
//import flash.events.MouseEvent;
public class Main extends MovieClip{
//These are all of the constants used.
const gravity:Number = 1.5; //gravity of the game. How fast ball falls
const dist_btw_obstacles:Number = 300; //distance between two obstacles
const ob_speed:Number = 8; //speed of the obstacle
const jump_force:Number = 15; //force with which it jumps
//variables
var player:Player = new Player();
var lastob:Obstacle = new Obstacle(); //varible to store the last obstacle in the obstacle array
var obstacles:Array = new Array(); //an array to store all the obstacles
var yspeed:Number = 0; //A variable representing the vertical speed of the bird
var score:Number = 0; //A variable representing the score
public function Main(){
init();
}
function init():void {
//initialize all the variables
player = new Player();
lastob = new Obstacle();
obstacles = new Array();
yspeed = 0;
score = 0;
//add player to center of the stage the stage
player.x = stage.stageWidth/2;
player.y = stage.stageHeight/2;
addChild(player);
//create 3 obstacles ()
createObstacle();
createObstacle();
createObstacle();
//Add EnterFrame EventListeners (which is called every frame) and KeyBoard EventListeners
addEventListener(Event.ENTER_FRAME,onEnterFrameHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, key_up);
}
private function key_up(event:KeyboardEvent){
if(event.keyCode == Keyboard.SPACE){
//If space is pressed then make the bird
yspeed = -jump_force;
}
}
function restart(){
if(contains(player))
removeChild(player);
for(var i:int = 0; i < obstacles.length; ++i){
if(contains(obstacles[i]) && obstacles[i] != null)
removeChild(obstacles[i]);
obstacles[i] = null;
}
obstacles.slice(0);
init();
}
function onEnterFrameHandler(event:Event){
//update player
yspeed += gravity;
player.y += yspeed;
//restart if the player touches the ground
if(player.y + player.height/2 > stage.stageHeight){
restart();
}
//Don't allow the sheep to go above the screen
if(player.y - player.height/2 < 0){
player.y = player.height/2;
}
//update obstacles
for(var i:int = 0;i<obstacles.length;++i){
updateObstacle(i);
}
//display the score
scoretxt.text = String(score);
}
//This functions update the obstacle
function updateObstacle(i:int){
var ob:Obstacle = obstacles[i];
if(ob == null)
return;
ob.x -= ob_speed;
if(ob.x < -ob.width){
//if an obstacle reaches left of the stage then change its position to the back of the last obstacle
changeObstacle(ob);
}
//If the bird hits an obstacle then restart the game
if(ob.hitTestPoint(player.x + player.width/2,player.y + player.height/2,true)
|| ob.hitTestPoint(player.x + player.width/2,player.y - player.height/2,true)
|| ob.hitTestPoint(player.x - player.width/2,player.y + player.height/2,true)
|| ob.hitTestPoint(player.x - player.width/2,player.y - player.height/2,true)){
restart();
}
//If the bird got through the obstacle without hitting it then increase the score
if((player.x - player.width/2 > ob.x + ob.width/2) && !ob.covered){
++score;
ob.covered = true;
}
}
//This function changes the position of the obstacle such that it will be the last obstacle and it also randomizes its y position
function changeObstacle(ob:Obstacle){
ob.x = lastob.x + dist_btw_obstacles;
ob.y = 100+Math.random()*(stage.stageHeight-200);
lastob = ob;
ob.covered = false;
}
//this function creates an obstacle
function createObstacle(){
var ob:Obstacle = new Obstacle();
if(lastob.x == 0)
ob.x = 800;
else
ob.x = lastob.x + dist_btw_obstacles;
ob.y = 100+Math.random()*(stage.stageHeight-200);
addChild(ob);
obstacles.push(ob);
lastob = ob;
}
}
}
Here is the code for my main page called "Scenery" (Scene 1) which is in an actions layer of timeline.
stop();
import flash.events.MouseEvent;
sun1.addEventListener(MouseEvent.CLICK,cloudy);
function cloudy (e:MouseEvent){
cloud1.x = cloud1.x + 10;
cloud2.x = cloud2.x - 10;
}
pong_btn.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
function mouseDownHandler(event:MouseEvent):void {
gotoAndStop(1, "SheepyPong");
}
dodge_btn.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler3);
function mouseDownHandler3(event:MouseEvent):void {
gotoAndStop(1, "Flappy");
}
This is my code for a very simple "SheepyPong" (Scene2) game which is linked to from front page:
var upPressed:Boolean = false;
var downPressed:Boolean = false;
var velocityX:Number = 5;
var velocityY:Number = 5;
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;
}
sheep.x += velocityX;
sheep.y += velocityY;
if(sheep.y + sheep.height/2 > stage.stageHeight || sheep.y - sheep.height/2 < 0){
velocityY *= -1
}
if(sheep.hitTestObject(AI) || sheep.hitTestObject(movieClip_1)){
velocityX *= -1;
}
if (sheep.x < 0) {
score2.text = (int(score2.text) +1).toString();
sheep.x = 275;
sheep.y = 200;
velocityX *= -1;
}
if (sheep.x > stage.stageWidth) {
score1.text = (int(score1.text) +1).toString();
sheep.x = 275;
sheep.y = 200;
velocityX *= -1;
}
AI.y = sheep.y;
}
function fl_SetKeyPressed(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.UP:
{
upPressed = true;
break;
}
case Keyboard.DOWN:
{
downPressed = true;
break;
}
}
}
function fl_UnsetKeyPressed(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.UP:
{
upPressed = false;
break;
}
case Keyboard.DOWN:
{
downPressed = false;
break;
}
}//end of switch
}//end of function
home_btn.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler1);
function mouseDownHandler1(event:MouseEvent):void {
gotoAndStop(1, "Scenery");
}
I'm also getting this error in output when I run it. It's not actually affecting the outpu
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Main/onEnterFrameHandler()[/Users/mynamehere/Documents/Game/Main.as:91]
//This is line 91 in the Main.as file:
scoretxt.text = String(score);
I'm currently trying to program a flash game for Android.
I have more or less working gravity & velocity and hit tests - so I don't fall through my platforms.
The problem now is, as soon as I hit "jump", the hit test stops working and i fall through the platforms. If I set my character to a different, higher position I don't even fall down.
Can anyone help me figure this out?
Here is my code:
import flash.events.MouseEvent;
import flash.events.Event;
var gravity:Number = 2;
var velocity:Number = 1.1;
var jumpPower:Number = 0;
var isJumping:Boolean = false;
stage.addEventListener(Event.ENTER_FRAME, touchPlatform);
player.addEventListener(Event.ENTER_FRAME, appeal);
function touchPlatform(e:Event):void
{
if(kurz.hitTestObject(player))
{
hitPlatform = true;
}
else if(kurz2.hitTestObject(player))
{
hitPlatform = true;
}
}
function appeal(e:Event):void
{
gravity *= velocity;
player.y += gravity;
if(hitPlatform == true)
{
velocity = 0;
}
}
jump.addEventListener(MouseEvent.CLICK, doJump);
stage.addEventListener(Event.ENTER_FRAME, update);
function doJump(e:MouseEvent):void
{
if(!isJumping)
{
jumpPower = 30;
isJumping = true;
}
}
function update(e:Event):void
{
if(isJumping)
{
player.y -= jumpPower;
jumpPower -= 2;
}
else
{
isJumping = false;
}
}
Your issue is that once you start jumping, you never stop! No where (that can be reached) do you set isJumping to false. Also, your jumping value and your gravity are currently running in tandem, you only want one OR the other affecting your player at any given time.
Try something like this (see code comments for explainations)
import flash.events.MouseEvent;
import flash.events.Event;
var gravity:Number = 2;
var velocity:Number = 1.1;
var jumpPower:Number = 0;
var isJumping:Boolean = false;
jump.addEventListener(MouseEvent.CLICK, doJump);
function doJump(e:MouseEvent):void {
if(!isJumping){
jumpPower = 30;
isJumping = true;
}
}
//Just one ENTER_FRAME handler is better,
//then you have more control over the order in which code gets run
//I've combined your three into one and called it gameLoop
stage.addEventListener(Event.ENTER_FRAME, gameLoop);
function gameLoop(e:Event):void {
//this is the same, I just combined your 2 if's into one.
if(kurz.hitTestObject(player) || kurz2.hitTestObject(player)){
hitPlatform = true;
}
//you should set the velocity before assigning it to the player
if(hitPlatform == true){
velocity = 0;
gravity = 2; //you probably want to reset gravity to default too
isJumping = false; //SET isJumping to false now that you've hit a platform! <-------------
}else{
velocity = 1.1; //you need to reset velocity when not on a platform
}
//Now that we've determined the velocity and if we're jumping, let's move the player the appropriate amount
if(isJumping){
//Since we're currently jumping, use the jumpPower instead of gravity
player.y -= jumpPower;
jumpPower -= 2;
}else{
if(!hitPlatform){
//Since we're NOT JUMPING, and not on a platform, use gravity.
gravity *= velocity;
player.y += gravity;
}
}
}
I would like to make the 5 'burger' objects bounce around the screen so they are harder to shoot as is the aim of my game. But, so far they are only lining up at the top of the stage so it's way too easy to play. Would I need to create 5 separate objects with 5 separate instance names etc.
This is what I have so far:
var firing:Boolean = false;
var bullet:Bullet1 = new Bullet1();
stage.addEventListener(KeyboardEvent.KEY_DOWN, keydown);
function keydown(event:KeyboardEvent):void {
switch(event.keyCode) {
case Keyboard.LEFT :
ball.x -= 10;
break;
case Keyboard.SPACE :
if (!firing) {
fire();
}
break;
case Keyboard.RIGHT :
ball.x += 10;
break;
case Keyboard.UP :
ball.y -= 10;
break;
case Keyboard.DOWN :
ball.y += 10;
break;
default :
break;
}
}
stage.addEventListener(KeyboardEvent.KEY_DOWN, keydown);
function fire() {
addChild(bullet);
firing = true;
bullet.x = ball.x;
bullet.y = ball.y - 60
;
}
addEventListener(Event.ENTER_FRAME, movestuff);
function movestuff(event:Event):void {
if (firing) {
bullet.y -= 20;
if (bullet.y < 0) {
firing = false;
removeChild(bullet);
}
}
}
var numBurger:Number = 5;
var array:Array = new Array();
for (var i:uint = 0; i<numBurger; i++) {
var burger:Burger = new Burger();
array.push(burger);
addChild(burger);
burger.x = 100 + 100*i;
burger.y = 50;
}
addEventListener(Event.ENTER_FRAME, checkCollision);
function checkCollision(event:Event)
{
for (var i:uint=0; i<array.length; i++)
{
if (array[i].hitTestObject(bullet))
{
removeChild(array[i]);
array.splice(i,1);
return;
}
}
}
Thanks for any help.
No, you would not have to create each movie clip separately if you use a loop to create randomized x and y locations for each burger. You can also use Math.random() to give a random speed and direction to each burger. In the code below these values are held in "direction_ary" array. This code creates five MovieClips of the "Burger"class, and places them at random points on the screen. The code also creates random speeds and directions for each MovieClip:
import flash.events.Event;
function find_random(max,min){
return Math.round(min+(max-min)*Math.random());
}
var ary:Array = [];
var direction_ary:Array = [];
for(var i:uint=0;i<5;i++){
ary[i]=new Burger();
ary[i].name="burger"+(i);
ary[i].x=find_random(stage.stageWidth-ary[i].width,ary[i].width);
ary[i].y=find_random(stage.stageHeight-ary[i].height,ary[i].height);
addChild(ary[i]);
direction_ary[i]=[find_random(5,-5),find_random(5,-5)];
for(var e:uint=0;e<100;e++){
if(direction_ary[i][0]==0||direction_ary[i][1]==0){
direction_ary[i]=[find_random(5,-5),find_random(5,-5)];
}else{
break;
}
}
}
stage.addEventListener(Event.ENTER_FRAME,update_burgers);
function update_burgers(e:Event){
for(var i:uint=0;i<5;i++){
if (ary[i].x>stage.stageWidth||ary[i].x<0){
direction_ary[i][0]*=-1;
}
if (ary[i].y>stage.stageHeight||ary[i].y<0){
direction_ary[i][1]*=-1;
}
ary[i].x+=direction_ary[i][0];
ary[i].y+=direction_ary[i][1];
}
}
The code is fairly self explanatory. Good luck with your project.
Cheers,
Drake Swartzy
so I haven't started quite yet, but I want to make a simple d-pad for my application in flash AS3. So let's just say I have four buttons. UpBtn, DownBtn, LeftBtn, and RightBtn. I want to simply just move an object in those directions. Let's say the objects name is "manD".
How would I do this in AS3?
You could do something like this (all your buttons do need a name and your man:
var speed:int = 10;
var xdir:int = 0;
var ydir:int = 0;
UpBtn.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
DownBtn.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
LeftBtn.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
RightBtn.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
addEventListener(MouseEvent.MOUSE_UP, onUp);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
// What happens when a button is released.
function onUp(e:MouseEvent):void {
xdir = 0;
ydir = 0;
}
// What is happening when one of the buttons is clicked.
function onDown(e:MouseEvent):void {
// Reset direction
onUp(e);
switch(e.target.name){
case 'LeftBtn':
xdir = -1;
break;
case 'RightBtn':
xdir = 1;
break;
case 'UpBtn':
ydir = -1;
break;
case 'DownBtn':
ydir = 1;
break;
}
}
// The actual movement.
function onEnterFrame(e:Event):void {
manD.x += xdir * speed;
manD.y += ydir * speed;
}
That should do it.
I'm trying to make sort of a Balloon fight game for flash. I have a ceiling at the top of the screen, so when the player hits it, it should bounce off of it. Problem is, I don't know how to go about doing that. Here is the code.
I was messing around with numbers. In the upBumping variable, where the problem lies, I wanted to have the character bounce down once it hits, but it only does this once. After, it just goes through the ceiling. Perhaps I'm going about this the wrong way?
//gravity and stuff
var gravity:Number = 0.3;
var fall:Number = 0.3;
var downBumping:Boolean = false;
var upBumping:Boolean = false;
//direction I guess
var leftKeyDown:Boolean = false;
var upKeyDown:Boolean = false;
var rightKeyDown:Boolean = false;
var spaceKeyDown:Boolean = false;
//character movement ya'll
var mainSpeed:Number = 6;
stage.addEventListener(Event.ENTER_FRAME, game);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
function game(e:Event): void{
//if certain keys are down then move dat fool
if(leftKeyDown){
Monkey.x -= mainSpeed;
}
if(rightKeyDown){
Monkey.x += mainSpeed;
}
if(upKeyDown){
fall = fall-0.9;
gravity = 0.3;
}
if(Floor.hitTestObject(Monkey)){
downBumping = true;
trace("downBumping");
}
if(Ceiling.hitTestObject(Monkey)){
upBumping = true;
trace("upBumping");
trace(Monkey.y);
}
if(upBumping){
Monkey.y += 20;
}
Monkey.y = Monkey.y+fall;
gravity = gravity*1.0;
fall = fall+gravity;
}
Here's how I would do jumping and bouncing - this does not include other code like moving left to right since you already got that
function game(e:Event):void {
if(jumping){
yVelocity += 0.1; //gravity
Monkey.y += yVelocity;
if(Floor.hitTestObject(Monkey)){
jumping = false;
while(Floor.hitTestObject(Monkey)){
y--;
}
}
if(Ceiling.hitTestObject(Monkey)){
yVelocity = 0;
while(Ceiling.hitTestObject(Monkey)){
y++;
}
}
}
if(upKeyDown && !jumping){
jumping = true;
yVelocity = -3;
}
}
Obviously, declare the variables used in this example.