To much FPS drop - actionscript-3

http://www.fastswf.com/USUAp00
When i scale my procedural generated map based off a perlin noise map to a bigger size, so i can place a character on the map and actually navigate it (moving the map around the player instead of the player around the map), it gets major fps drop.
Test for yourself with the provided link, the higher the scale or map width/height is increased the more it lags while walking. I understand this is allot of data blocks to move
Is there a better way to go about moving my player around?
Would adding the spawned objects to an object pool help? I don't think it would
Is it possible to split the map into segments after generation and load certain segments based off your x,y?
World class: //handles spawning the map
http://pastebin.com/CfMDBWeR
Level class: // handles moving the map around
public class Level extends MovieClip
{
var world:World;
protected var goin:Vector.<int> = new Vector.<int>();
protected var moveSpeed:int;
protected var MAP_SCALE:int;
public function Level()
{
MAP_SCALE = GlobalCode.MAP_SCALE;
trace("level")
for(var i:int = 0; i < 4; i++){
goin[i]=0;
}
world = new World(this);
addEventListener(Event.ENTER_FRAME, update);
addEventListener(Event.ADDED_TO_STAGE, init);
addEventListener(Event.REMOVED_FROM_STAGE, cleanUp);
moveSpeed = 10;
// constructor code
}
protected function init(e:Event)
{
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
}
protected function cleanUp(e:Event)
{
stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
stage.removeEventListener(KeyboardEvent.KEY_UP, keyReleased);
}
protected function keyPressed(k:KeyboardEvent)
{
if (k.keyCode == Keyboard.W)
{
goin[0] = 1;
}
if (k.keyCode == Keyboard.S)
{
goin[1] = 1;
}
if (k.keyCode == Keyboard.A)
{
goin[2] = 1;
}
if (k.keyCode == Keyboard.D)
{
goin[3] = 1;
}
}
protected function MovePlayer(){
if (goin[0] == 1)
{
world.worldTiles.y += moveSpeed;
}
if (goin[1] == 1)
{
world.worldTiles.y -= moveSpeed;
}
if (goin[2] == 1)
{
world.worldTiles.x += moveSpeed;
}
if (goin[3] == 1)
{
world.worldTiles.x -= moveSpeed;
}
}
protected function keyReleased(k:KeyboardEvent)
{
if (k.keyCode == Keyboard.W)
{
goin[0] = 0;
}
if (k.keyCode == Keyboard.S)
{
goin[1] = 0;
}
if (k.keyCode == Keyboard.A)
{
goin[2] = 0;
}
if (k.keyCode == Keyboard.D)
{
goin[3] = 0;
}
}
public function update(e:Event)
{
world.worldTiles.scaleX = MAP_SCALE;
world.worldTiles.scaleY = MAP_SCALE;
MovePlayer();
}
}
Stage Class: The class actually connected to the stage, handles frame switching
public class Stage extends MovieClip {
private var targetFrame:String;
public function Stage() {
targetFrame = "Menu";
// constructor code
}
private function gotoLabel(desiredLabel:String)
{
for (var i=0; i<currentLabels.length; i++)
{
if (currentLabels[i].name == desiredLabel)
{
gotoAndPlay(desiredLabel);
return;
}
}
trace("Requested frame missing!");
}
public function generateMap(m:MouseEvent){
GlobalCode.TILE_SIZE = int(tileSizeText.text);
GlobalCode.MAP_HEIGHT = int(mapHeightText.text);
GlobalCode.MAP_WIDTH = int(mapWidthText.text);
GlobalCode.MAP_SCALE = int(mapScaleText.text);
gotoLabel("Game");
}
private function doneLoading(m:MouseEvent)
{
//set targetFrame
gotoLabel("Menu");
}
}

Related

How can I change Nested Class in actionscript 3

I'm working off an actionscript 3 code that pulls information from other actionscript 3's. The main code is Herbfight and it references 4 others (Bullets, bugs, Goodbugs and Sprayer). Below I'm trying to combine them all into one actionscript. The reason for this is that I want to pull it in as a symbol rather then having the flash file relate to the class.
The problem is it says I can't have nested classes. Any thoughts on how i can fix this? Below is my attempt to combine the code.
Thanks,
J
package {
import flash.display.;
import flash.events.;
import flash.utils.Timer;
import flash.text.TextField;
import flash.text.*;
import flash.utils.getTimer;
import flash.geom.Point;
public class herbfight_v004 extends MovieClip {
private var aagun:Sprayer;
private var airplanes:Array;
private var goodbug:Array;
private var bullets:Array;
private var upArrow, downArrow:Boolean;
private var nextPlane:Timer;
private var nextGbugs:Timer;
private var shotsLeft:int;
private var shotsHit:int;
private var Sprayer:MovieClip
private var Bullets:MovieClip
private var bugs:MovieClip
private var GooodBugs:MovieClip
public function startherbfight_v004() {
// init score
shotsLeft = 20;
shotsHit = 0;
showGameScore();
// create gun
aagun = new Sprayer();
addChild(aagun);
// create object arrays
airplanes = new Array();
bullets = new Array();
goodbug = new Array();
// listen for keyboard
stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDownFunction);
stage.addEventListener(KeyboardEvent.KEY_UP,keyUpFunction);
// look for collisions
addEventListener(Event.ENTER_FRAME,checkForHits);
// start planes flying
setNextPlane();
setNextGbugs();
public function Sprayer() {
{
// animation time// initial location of gun
this.x = 410;
this.y = 380;
// movement
addEventListener(Event.ENTER_FRAME,moveGun);
}
public function moveGun(event:Event) {
// get time difference
var timePassed:int = getTimer()-lastTime;
lastTime += timePassed;
// current position
var newy = this.y;
// move to the left
if (MovieClip(parent).upArrow) {
newy -= speed*timePassed/1000;
}
// move to the right
if (MovieClip(parent).downArrow) {
newy += speed*timePassed/1000;
}
// check boundaries
if (newy < 65) newy = 65;
if (newy > 380) newy = 380;
// reposition
this.y = newy;
}
// remove from screen and remove events
public function deleteGun() {
parent.removeChild(this);
removeEventListener(Event.ENTER_FRAME,moveGun);
}
}
public function bugs(side:String, speed:Number, altitude:Number) {
{
if (side == "left") {
this.x = -50; // start to the left
dx = speed; // fly left to right
this.scaleX = 1; // reverse
} else if (side == "right") {
this.x = -50; // start to the right
dx = -speed; // fly right to left
this.scaleX = 1; // not reverse
}
this.y = altitude; // vertical position
// choose a random plane
this.gotoAndStop(Math.floor(Math.random()*4+1));
// set up animation
addEventListener(Event.ENTER_FRAME,movePlane);
lastTime = getTimer();
}
public function movePlane(event:Event) {
// get time passed
var timePassed:int = getTimer()-lastTime;
lastTime += timePassed;
// move plane
this.x += dx*timePassed/2000;
// check to see if off screen
if ((dx < 0) && (x < -50)) {
deletePlane();
} else if ((dx > 0) && (x > 350)) {
deletePlane();
}
}
// plane hit, show explosion
public function planeHit() {
removeEventListener(Event.ENTER_FRAME,movePlane);
MovieClip(parent).removePlane(this);
gotoAndPlay("explode");
}
// delete plane from stage and plane list
public function deletePlane() {
removeEventListener(Event.ENTER_FRAME,movePlane);
MovieClip(parent).removePlane(this);
parent.removeChild(this);
}
}
public function Bullets(x,y:Number, speed: Number) {
{
// set start position
this.x = x;
this.y = y;
// get speed
dx = speed;
// set up animation
lastTime = getTimer();
addEventListener(Event.ENTER_FRAME,moveBullet);
}
public function moveBullet(event:Event) {
// get time passed
var timePassed:int = getTimer()-lastTime;
lastTime += timePassed;
// move bullet
this.x += dx*timePassed/1000;
// bullet past top of screen
if (this.x < 0) {
deleteBullet();
}
}
// delete bullet from stage and plane list
public function deleteBullet() {
MovieClip(parent).removeBullet(this);
parent.removeChild(this);
removeEventListener(Event.ENTER_FRAME,moveBullet);
}
}
public function GoodBugs(side:String, speed:Number, altitude:Number) {
{
if (side == "left") {
this.x = -50; // start to the left
dx = speed; // fly left to right
this.scaleX = -1; // reverse
} else if (side == "right") {
this.x = -50; // start to the right
dx = -speed; // fly right to left
this.scaleX = -1; // not reverse
}
this.y = altitude; // vertical position
// choose a random Gbugs
this.gotoAndStop(Math.floor(Math.random()*2+1));
// set up animation
addEventListener(Event.ENTER_FRAME,moveGbugs);
lastTime = getTimer();
}
public function moveGbugs(event:Event) {
// get time passed
var timePassed:int = getTimer()-lastTime;
lastTime += timePassed;
// move Gbugs
this.x += dx*timePassed/2000;
// check to see if off screen
if ((dx < 0) && (x < -50)) {
deleteGbugs();
} else if ((dx > 0) && (x > 350)) {
deleteGbugs();
}
}
// Gbugs hit, show explosion
public function GbugsHit() {
removeEventListener(Event.ENTER_FRAME,moveGbugs);
MovieClip(parent).removeGbugs(this);
gotoAndPlay("explode");
}
// delete Gbugs from stage and Gbugs list
public function deleteGbugs() {
removeEventListener(Event.ENTER_FRAME,moveGbugs);
MovieClip(parent).removeGbugs(this);
parent.removeChild(this);
}
}
public function setNextPlane() {
nextPlane = new Timer(1000+Math.random()*1000,1);
nextPlane.addEventListener(TimerEvent.TIMER_COMPLETE,newPlane);
nextPlane.start();
}
public function newPlane(event:TimerEvent) {
// random side, speed and altitude
if (Math.random() > .2) {
var side:String = "left";
} else {
side = "right";
}
var altitude:Number = Math.random()*280+80;
var speed:Number = Math.random()*150+150;
// create plane
var p:bugs = new bugs(side,speed,altitude);
addChild(p);
airplanes.push(p);
// set time for next plane
setNextPlane();
}
public function setNextGbugs() {
nextGbugs = new Timer(1000+Math.random()*1000,1);
nextGbugs.addEventListener(TimerEvent.TIMER_COMPLETE,newGbugs);
nextGbugs.start();
}
public function newGbugs(event:TimerEvent) {
// random side, speed and altitude
if (Math.random() > .2) {
var side:String = "left";
} else {
side = "right";
}
var altitude:Number = Math.random()*280+80;
var speed:Number = Math.random()*150+150;
// create Gbugs
var p:GoodBugs = new GoodBugs(side,speed,altitude);
addChild(p);
goodbug.push(p);
// set time for next Gbugs
setNextGbugs();
}
// check for collisions
public function checkForHits(event:Event) {
for(var bulletNum:int=bullets.length-1;bulletNum>=0;bulletNum--){
for (var airplaneNum:int=airplanes.length-1;airplaneNum>=0;airplaneNum--) {
if (bullets[bulletNum].hitTestObject(airplanes[airplaneNum])) {
airplanes[airplaneNum].planeHit();
bullets[bulletNum].deleteBullet();
shotsHit++;
showGameScore();
break;
}
}
}
for(var bulletNum:int=bullets.length-1;bulletNum>=0;bulletNum--){
for (var GoodBugsNum:int= goodbug.length-1; GoodBugsNum>=0; GoodBugsNum--) {
if (bullets[bulletNum].hitTestObject(goodbug [GoodBugsNum])) {
goodbug [GoodBugsNum]. GbugsHit();
bullets[bulletNum].deleteBullet();
shotsHit--;
showGameScore();
break;
}
}
}
if ((shotsLeft == 0) && (bullets.length == 0)) {
endGame();
}
}
// key pressed
public function keyDownFunction(event:KeyboardEvent) {
if (event.keyCode == 38) {
upArrow = true;
} else if (event.keyCode == 40) {
downArrow = true;
} else if (event.keyCode == 32) {
fireBullet();
}
}
// key lifted
public function keyUpFunction(event:KeyboardEvent) {
if (event.keyCode == 38) {
upArrow = false;
} else if (event.keyCode == 40) {
downArrow = false;
}
}
// new bullet created
public function fireBullet() {
if (shotsLeft <= 0) return;
var b:Bullets = new Bullets(aagun.x,aagun.y,-300);
addChild(b);
bullets.push(b);
shotsLeft--;
showGameScore();
}
public function showGameScore() {
showScore.text = String("Score: "+shotsHit);
showShots.text = String("Shots Left: "+shotsLeft);
}
// take a plane from the array
public function removePlane(plane:bugs) {
for(var i in airplanes) {
if (airplanes[i] == plane) {
airplanes.splice(i,1);
break;
}
}
}
// take a plane from the array
public function removeGbugs(Gbugs:GoodBugs) {
for(var i in goodbug) {
if (goodbug[i] == Gbugs) {
goodbug.splice(i,1);
break;
}
}
}
// take a bullet from the array
public function removeBullet(bullet:Bullets) {
for(var i in bullets) {
if (bullets[i] == bullet) {
bullets.splice(i,1);
break;
}
}
}
// game is over, clear movie clips
public function endGame() {
// remove planes
for(var i:int=airplanes.length-1;i>=0;i--) {
airplanes[i].deletePlane();
}
for(var i:int= goodbug.length-1;i>=0;i--) {
goodbug [i].deleteGbugs();
}
airplanes = null;
goodbug = null
aagun.deleteGun();
aagun = null;
stage.removeEventListener(KeyboardEvent.KEY_DOWN,keyDownFunction);
stage.removeEventListener(KeyboardEvent.KEY_UP,keyUpFunction);
removeEventListener(Event.ENTER_FRAME,checkForHits);
nextPlane.stop();
nextPlane = null;
nextGbugs.stop();
nextGbugs = null;
gotoAndStop("gameover");
}
}
}

AS3 - how to remove a child when it reaches a certain point, and re add the child

So first off i'd like to state that i am not very good at AS3, i'm completely self taught so i am sure that there are many things i've done badly, inefficiently or plain wrong and i'm happy for any comments on these if there is things i can improve.
In addition, i have done this with classes, however i am now running into a time problem and have decided to make things work first and sort out the class files properly later, i know this is also very bad practice and makes more work than needed, however as i'm not so confident in their use, i would rather complete the work.
So to my question, i am creating a platform game and so far i've got the movement and jumping from platform to platform down, and i am trying to add the scrolling functionality of the game now, the way i am attempting this is by removing a child when it reaches the bottom of the screen, and then adding that child back in a random position.
on the start function i have this array to add the child and randomize it's position:
for(i=0;i<8;i++) {
PlatformInstance[i] =new Platform();
PlatformInstance[i].y= Math.random()*900;
PlatformInstance[i].x= Math.random() *1500;
stage.addChild(PlatformInstance[i]);
}
the code for my collisions and the scrolling is shown below:
for (i=0; i<8; i++) {
if (PlatformInstance[i].hitTestPoint (Smallclock_hero.x,Smallclock_hero.y+130,true)) {
yescollision = true;
if (keyboard_input.is_up() && yescollision == true)
{
trace ("boop")
Smallclock.y_speed = -40
yescollision = false;
}
else {
Smallclock.y_speed *= 0;
}
}
if (Scrolling == true) {
PlatformInstance[i].y += 1; // this moves the platforms down.
}
}
is there a simple and easy way to remove the PlatformInstance when it reaches a point say (1000) and then add the instance again, with the same randomization code?
thanks in advance.
Adding all of my class files for clarities sake, not sure if you will need them
Start.as
package com {
import flash.display.MovieClip;
import flash.events.Event;
public class Start extends MovieClip {
public var keyboard_input:Keys;
public var Smallclock_hero = new Smallclock;
public var BasePlatformInstance:BasePlatform = new BasePlatform;
public var BackgroundInstance:Background = new Background();
public var PlatformInstance:Platform = new Platform();
public var keyboard_sprite = new MovieClip();
public static var yescollision:Boolean = false
var yScrollSpeed:int = 1;
var Scrolling:Boolean = false;
var i:int =0;
public var Running:Boolean = true;
public function Start () {
trace("Hello")
addChild (BackgroundInstance);
addChild (Smallclock_hero);
addChild (BasePlatformInstance);
BasePlatformInstance.x = 0;
BasePlatformInstance.y = 980;
BackgroundInstance.height = stage.stageHeight +50 ;
BackgroundInstance.width = stage.stageWidth +50 ;
Smallclock_hero.init();
var keyboard_sprite = new MovieClip();
addChild (keyboard_sprite);
keyboard_input = new Keys (keyboard_sprite);
stage.addEventListener(Event.ENTER_FRAME,on_enter);
addEventListener (Event.ENTER_FRAME, collisions);
//addEventListener (Event.ENTER_FRAME,refreshPlatform)
for(i=0;i<8;i++) {
PlatformInstance[i] =new Platform();
PlatformInstance[i].y= Math.random()*900;
PlatformInstance[i].x= Math.random() *1500;
stage.addChild(PlatformInstance[i]);
}
}
public function on_enter(event:Event) {
if (keyboard_input.is_left()){
Smallclock_hero.apply_force(-1,0);
Smallclock_hero.scaleX = -1
}
if (keyboard_input.is_right()) {
Smallclock_hero.apply_force(1,0);
Smallclock_hero.scaleX = 1
}
}
public function collisions (e:Event) {
if (BasePlatformInstance.hitTestPoint (Smallclock_hero.x,Smallclock_hero.y+130, true)) {
//trace ("touching!")
yescollision = true;
if (keyboard_input.is_up())
{
Smallclock.y_speed = -40
Start.yescollision = false;
}
if (Smallclock.y_speed ==-40) {
Scrolling = true;
removeChild(BasePlatformInstance);
}
else {
Smallclock.y_speed *= 0;
}
}
for (i=0; i<8; i++) {
if (PlatformInstance[i].hitTestPoint (Smallclock_hero.x,Smallclock_hero.y+130, true)) {
yescollision = true;
if (keyboard_input.is_up() && yescollision == true)
{
trace ("boop")
Smallclock.y_speed = -40
yescollision = false;
}
else {
Smallclock.y_speed *= 0;
}
}
if (Scrolling == true) {
PlatformInstance[i].y += 1;
}
}
Smallclock.as
package com {
import flash.display.Sprite;
import flash.events.Event;
public class Smallclock extends Sprite {
private var x_speed:Number;
public static var y_speed:Number;
private var power:Number;
public var friction:Number;
public static var gravity:Number;
public static var jumping:Boolean = false;
public static var jumppwr:Number;
public static var jumpSpeedLimit:int = 15;
public function Smallclock() {
addEventListener (Event.ENTER_FRAME, move);
}
private function move (e:Event) {
x+=x_speed;
y+=y_speed;
y_speed += gravity
x_speed *= friction ;
y_speed *= friction ;
if (x < -25) {
x = 1650
}
if (x > 1650) {
x = -25
}
}
public function apply_force (x_force,y_force){
x_speed += (x_force*power);
y_speed += (y_force*power);
}
public function init() {
jumppwr = 2;
gravity = 1.0;
power = 0.8;
friction = 0.9;
x_speed = 0;
y_speed = 0;
x= stage.stageWidth/2;
y = 850;
}
}
}
Keys.as
package com {
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class Keys {
private var press_left = false;
private var press_right = false;
private var press_up = false;
private var press_down = false;
private var press_space = false;
public function Keys(movieclip) {
movieclip.stage.addEventListener(KeyboardEvent.KEY_DOWN, key_down);
movieclip.stage.addEventListener(KeyboardEvent.KEY_UP, key_up);/**/
}
public function is_left() {
return press_left;
}
public function is_right() {
return press_right;
}
public function is_up() {
return press_up;
}
public function is_down() {
return press_down;
}
public function is_space() {
return press_space;
}
public function key_down(event:KeyboardEvent) {
if (event.keyCode == 32) {
press_space = true;
}
if (event.keyCode == 37) {
press_left = true;
}
if (event.keyCode == 38) {
press_up = true;
}
if (event.keyCode == 39) {
press_right = true;
}
if (event.keyCode == 40) {
press_down = true;
}
}
public function key_up(event:KeyboardEvent) {
if (event.keyCode == 32) {
press_space = false;
}
if (event.keyCode == 37) {
press_left = false;
}
if (event.keyCode == 38) {
press_up = false;
}
if (event.keyCode == 39) {
press_right = false;
}
if (event.keyCode == 40) {
press_down = false;
}
}
}
}
I am not going to read thru all your code, sorry.
Of course there is. In the interval/event handler where you are updating your positions, you have to loop through all your platforms and check whether their position is >= 1000. If it is, you don't need to remove it, just randomize it and set its position again with the code you already have:
for(i=0;i<8;i++) {
if(PlatformInstance[i].x >= 1000) {
var inst:Platform = PlatformInstance[i];
inst.y= Math.random() * 900;
inst.x= Math.random() * 1500;
}
That should work just fine. If you really need to remove it (??), you can do so with stage.removeChild(inst) and then stage.addChild(inst) again.
A few tips from that few code I read in your question: do not give instance/variable names with capitalized first letter. That should be a class name (PlatformInstance is obviously an instance of Array or Vector, use platformInstance). Do not add object directly to stage. If possible, add them to your stage owner (which is your document class if you write your code in classes).

Action Script 3. Platform game physics glitch (gravity)

when my player touches the floor, he is unable to move because he is on the floor and being incremented up.
In the main class I have the movement
private function processMovement():void
{
if (touchingGround)
{
if (upKey)
{
character.jumpUp();
}
if (leftKey)
{
character.moveLeft();
}
if (rightKey)
{
character.moveRight();
}
if (!leftKey && !rightKey && !upKey)
{
character.dontMove();
}
}
}
Then in the character class you will see this.
public class player extends OnGround
{
public var canJumpAn:Boolean;
public var attackAn:Boolean;
public var jumpheight:Number = 18;
public function player()
{
addEventListener(Event.ADDED_TO_STAGE, onAdd);
}
private function onAdd(e: Event): void
{
removeEventListener(Event.ADDED_TO_STAGE, onAdd);
}
public function moveLeft():void
{
//decrease VELOCITY
xV -= 2;
if (xV > -7)
{
xV = -7;
}
this.gotoAndStop("run");
//add this to the Mc . x pos baby.
this.x += xV;
this.scaleX = -1;
charIsrunning = true;
}
public function moveRight():void
{
//increase VELOCITY
xV += 2;
if (xV > 7)
{
xV = 7;
}
this.gotoAndStop("run");
//add this to the Mc . x pos baby.
this.x += xV;
this.scaleX = 1;
charIsrunning = true;
}
public function dontMove():void
{
//if no button presses then do this
this.gotoAndStop("stop");
//slowd down ball
xV *= friction;
charIsrunning = false;
isDefending = false;
//stop the ball if you're not moving
if (xV > - 1 && xV < 1)
{
xV = 0;
}
}
override public function positionOnLand():void
{
isJumping = false;
///=gotoAndStop(1);
}
public function defend():void
{
isDefending = true;
this.gotoAndStop("defend");
charIsrunning = false;
}
public function attack():void
{
this.gotoAndStop("attack");
}
public function jumpUp():void
{
if (!isJumping)
{
isJumping = true;
this.gotoAndStop("jump");
//
}
}
}
}
OnGround is another class that player extends, as you can see.
public class OnGround extends MovieClip
{
public var grav:Number;
public var friction:Number;
public var xV:Number;
public var yV:Number;
protected var charIsrunning:Boolean;
protected var isDefending:Boolean;
protected var isJumping:Boolean;
public function OnGround()
{
addEventListener(Event.ADDED_TO_STAGE, init)
charIsrunning = false;
isDefending = false;
//gotoAndStsop("jump");
}
private function init(e:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
//gravity
grav = 0.6;
//y velocity
yV = 0;
//x velocity
xV = 0;
//
friction = 0.9;
addEventListener(Event.ENTER_FRAME, fall);
}
private function fall(e:Event):void
{
//add grav to y VELOCITY
yV += grav;
trace(yV);
this.y += yV
}
public function incrementUp():void
{
this.y -= 0.1;
//trace("incrementing");
}
public function keepOnGround():void
{
//trace("onGroundBitch");
grav = 0;
yV = 0;
positionOnLand();
}
public function positionOnLand():void
{
//overide
}
}
}
This is a function that's in the main class
for (var c:int = 0; c < childrenOnStage; c++)
{
if (getChildAt(c).name == "player")
{
if (ground.level1Ground.hitTestPoint(getChildAt(c).x + 13, getChildAt(c).y, true) || ground.level1Ground.hitTestPoint(getChildAt(c).x - 13, getChildAt(c).y, true))
{
getChildAt(c).y --;
//OnGround(getChildAt(c)).incrementUp();
OnGround(getChildAt(c)).keepOnGround();
touchingGround = true;
}
else
{
touchingGround = false;
}
The problem is that when the player touches the ground then it's y position is incremented until it isn't touching the ground and then it's suppose to be kept on the ground by turning grav to = 0 and y velocity to 0.
This means gravity is turned off and the players y position will not shift up (when I jump)
or when go down to be kept on ground.
I would appreciate it if someone can lend me a hand or point me in the correct direction.
I'm not sure why you'd have a gravity value per object unless you really intend to have different object instances have individual gravities.
If not I'd reference a gravity constant instead.
Is anti-gravity a feature of the game? if not then I'd avoid setting gravity to 0. It may 'work' (to an extent) but it's not a good model/abstraction of reality.
Rather, what I would do is seperate the force applied and the current momentum:
The (downward) force of gravity is applied at all times.
When you're standing on the ground an upward force equal and opposite to gravity is applied.
When you jump, add a one-off instance of upward force.
Use the current forces to change the current momentum and use the current momentum to change the position.

How to detect if a player is on a platform

I am making a basic platform game using as3, and am trying to find a way to detect if my player is on the platform so I can allow him to jump if he is on it, but not be able to jump if he's off the platform.Here is my code:
package {
import flash.display.MovieClip
import flash.events.Event
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class Main extends MovieClip
{
var _vx = 0
var _vy = 0
var _ay = 0.5
var _isOnGround:Boolean
var canJump:Boolean
var _collisionArea:MovieClip
// Constants:
// Public Properties:
// Private Properties:
// Initialization:
public function Main()
{
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp)
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
function onKeyDown(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.LEFT)
{
_vx = -5;
}
else if (event.keyCode == Keyboard.RIGHT)
{
_vx = 5;
}
else if (event.keyCode == Keyboard.UP )
{
if (canJump)
{
_vy = -10
}
}
}
function onKeyUp(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.LEFT)
{
_vx = 0;
}
else if (event.keyCode == Keyboard.RIGHT)
{
_vx = 0;
}
}
public function onEnterFrame(event:Event):void
{
if(player1.isOnGround)
{
canJump=true
}
else if (!player1.isOnGround)
{
canJump=false
}
if (_vy > 10)
{
_vy = 10
}
player1.y += _vy
player1.x += _vx
vy += _ay
//Player vs wall collision
for (var i:int = 0; i <= 4; i++)
{
CollisionB.platform_BlockRectangles(player1, this["wall" + i]);
//trace("wall" + i);
}
}
public function set vx(vxValue:Number):void
{
_vx = vxValue;
}
public function get vx():Number
{
return _vx;
}
public function set vy(vyValue:Number):void
{
_vy = vyValue;
}
public function get vy():Number
{
return _vy;
}
public function get isOnGround():Boolean
{
return _isOnGround;
}
public function set isOnGround(onGround:Boolean):void
{
_isOnGround = onGround;
}
public function get collisionArea():MovieClip
{
return _collisionArea;
}
}
}
Im also using another class where the code is:
static public function platform_BlockRectangles(objectA:Object,objectB:Object):void
{
//This function requires the following setter properties in objectA:
// _objectIsOnGround:Boolean, vx:Number, vy:Number
var objectA_Halfwidth=objectA.width/2;
var objectA_Halfheight=objectA.height/2;
var objectB_Halfwidth=objectB.width/2;
var objectB_Halfheight=objectB.height/2;
var dx=objectB.x-objectA.x;
var ox=objectB_Halfwidth+objectA_Halfwidth-Math.abs(dx);
if (0<ox)
{
var dy=objectA.y-objectB.y;
var oy=objectB_Halfheight+objectA_Halfheight-Math.abs(dy);
if (0<oy)
{
if (ox<oy)
{
if (dx<0)
{
ox*=-1;
oy=0;
}
else
{
oy=0;
}
//Dampen horizontal velocity
objectA.vx=0;
}
else
{
if (dy<0)
{
ox=0;
oy*=-1;
objectA.isOnGround=true;
}
else
{
ox=0
}
//Dampen vertical velocity
objectA.vy=0;
}
objectA.x-=ox;
objectA.y+=oy;
}
}
}
Someone please help!!!!!!!
I extremely recommend you to use some library like: Flixel, Citrus
Try to follow this tutorial, will explain all necessary logic to help you with your game
You can use AABB collision detection. Here is a tutorial (the Distance function part).
If you google the term you will get a lot of info in the subject.
But i recomend you to use flixel or flashPunk is you are just starting and want to do games, they take care of these stuff for you and you can get to just make the game.

Actionscript 3 returns Error #1009

I am creating a shooting game. I have encountered a problem with my code when I decided to create a method to remove the missile after reaching the top of the stage. I can run the program without any issues only I have realize that the missile was not remove away from the stage, if I hold the shooting button. However, if I tap the shooting button, the missile will removed away with this error #1009 printing out of the output.
Is there any solution to fix the problem?
Here's is the error after the missile flew to the top of the stage with debugging enabled:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Missle/destroyMissle()[E:\Experiment\ExperimentProject\Missle.as:39]
at main/checkMissleOffScreen()[E:\Experiment\ExperimentProject\main.as:63]
at main/eventUpdated()[E:\Experiment\ExperimentProject\main.as:51]
Here's the main's class:
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;
/**
* ...
* #author test
*/
public class main extends MovieClip
{
//Objects
public var rect:MovieClip;
public var missle:Missle;
//Array
private var missleArray:Array;
//Keyboard section
var leftKeyIsDown:Boolean;
var rightKeyIsDown:Boolean;
var upKeyIsDown:Boolean;
var downKeyIsDown:Boolean;
var spaceKeyIsDown:Boolean;
//Speed
var characterSpeed:Number = 15;
//Main constructor
public function main()
{
//Array initializer
missleArray = new Array();
missle = new Missle();
//Update events listeners.
stage.addEventListener(Event.ENTER_FRAME, eventUpdated);
//Update keyboard events listeners
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUnpressed);
}
//Events functions
//This functions updated everytime the object is move
private function eventUpdated(e:Event):void
{
playerMoving();
playerClampMoving();
checkMissleOffScreen();
}
private function checkMissleOffScreen():void
{
for (var i = 0; i < missleArray.length; i++ )
{
var currentMissle:Missle = missleArray[i];
if (currentMissle.y < 0)
{
missleArray.splice(i, 1);
missle.destroyMissle();
}
}
}
private function playerClampMoving():void
{
if (rect.x < 0)
{
rect.x = 0;
}
if (rect.x > stage.stageWidth - rect.width)
{
rect.x = stage.stageWidth - rect.width;
}
if (rect.y < 0)
{
rect.y = 0;
}
if (rect.y > stage.stageHeight - rect.height)
{
rect.y = stage.stageHeight - rect.height;
}
}
private function playerMoving():void
{
if (leftKeyIsDown == true)
{
rect.x -= characterSpeed;
}
if (rightKeyIsDown == true)
{
rect.x += characterSpeed;
}
if (upKeyIsDown == true)
{
rect.y -= characterSpeed;
}
if (downKeyIsDown == true)
{
rect.y += characterSpeed;
}
if (spaceKeyIsDown == true)
{
shootingMissle();
}
}
private function shootingMissle():void
{
missle = new Missle();
missle.x = rect.x + (rect.width / 2);
missle.y = rect.y;
missleArray.push(missle);
trace(missleArray.length);
stage.addChild(missle);
}
//Keyboard functions
//Check to see whether the user releases the keyboard
private function keyUnpressed(e:KeyboardEvent):void
{
if (e.keyCode == 37)
{
leftKeyIsDown = false;
}
if (e.keyCode == 39)
{
rightKeyIsDown = false;
}
if (e.keyCode == 40)
{
downKeyIsDown = false;
}
if (e.keyCode == 38)
{
upKeyIsDown = false;
}
if (e.keyCode == 32)
{
spaceKeyIsDown = false;
}
}
//Check to see whether the user presses the keyboard
private function keyPressed(e:KeyboardEvent):void
{
if (e.keyCode == 37)
{
leftKeyIsDown = true;
}
if (e.keyCode == 39)
{
rightKeyIsDown = true;
}
if (e.keyCode == 40)
{
downKeyIsDown = true;
}
if (e.keyCode == 38)
{
upKeyIsDown = true;
}
if (e.keyCode == 32)
{
spaceKeyIsDown = true;
}
}
}
}
Here's the Missle's Class:
package
{
import flash.display.Sprite;
import flash.events.Event;
/**
* ...
* #author test
*/
public class Missle extends Sprite
{
public function Missle()
{
addEventListener(Event.ADDED_TO_STAGE, onAdd);
}
private function onAdd(e:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, onAdd);
//Objects are on the stage
init();
}
private function init():void
{
addEventListener(Event.ENTER_FRAME, missleLaunch);
}
private function missleLaunch(e:Event):void
{
this.y -= 15;
}
public function destroyMissle():void
{
parent.removeChild(this);
removeEventListener(Event.ENTER_FRAME, missleLaunch);
}
}
}
Try this:
public function destroyMissle():void
{
if(parent !== null) parent.removeChild(this);
removeEventListener(Event.ENTER_FRAME, missleLaunch);
}
It is a possibility that you were calling .destroyMissile() more than once meaning parent would be null because you've removed it from the stage and it has no parent.