i am in the process of making a platformer in AS3. However, one thing im struggling with is how to manage slope detection. Preferably id like something similar to that of games like fancy pants adventures, but id really prefer not to use an external thing such as Box2D or CDK, because both of which confuse me greatly, and id generally prefer just to not have one. All my attempts have had the problem that i can get it to work with some slopes but not all (e.g character manages up one slope, but falls through the other) then when i change it to suit the other, it doesnt suit the first one (e.g player manages the second slope, but bounces up and down crazily on the second...
So, any help would be greatly appreciated...
Thanks.
Code is as follows.
//var setup
var leftPressed: Boolean = false;
var rightPressed: Boolean = false;
var upPressed: Boolean = false;
var downPressed: Boolean = false;
var leftBumping: Boolean = false;
var rightBumping: Boolean = false;
var upBumping: Boolean = false;
var downBumping: Boolean = false;
var lowerleftBumping: Boolean = false;
var lowerrightBumping: Boolean = false;
var leftBumpPoint: Point = new Point(-30, -87);
var rightBumpPoint: Point = new Point(30, -87);
var lowerleftBumpPoint: Point = new Point(-32, -2);
var lowerrightBumpPoint: Point = new Point(32, -2);
var upBumpPoint: Point = new Point(0, -174);
var downBumpPoint: Point = new Point(0, 0);
var scrollX: Number = -4;
var scrollY: Number = -68;
var ySpeed: Number = 0;
var xSpeed: Number = 0;
var speedConstant: int = 5;
var maxSpeedConstant: Number = 30;
var frictionConstant: Number = 0.9;
var gravityConstant: Number = 2;
var jumpConstant: Number = -45;
//game
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
stage.addEventListener(Event.ENTER_FRAME, loop);
function keyDownHandler(e: KeyboardEvent): void {
if (e.keyCode == Keyboard.LEFT) {
leftPressed = true;
} else if (e.keyCode == Keyboard.RIGHT) {
rightPressed = true;
} else if (e.keyCode == Keyboard.UP) {
upPressed = true;
} else if (e.keyCode == Keyboard.DOWN) {
downPressed = true;
}
}
function keyUpHandler(e: KeyboardEvent): void {
if (e.keyCode == Keyboard.LEFT) {
leftPressed = false;
} else if (e.keyCode == Keyboard.RIGHT) {
rightPressed = false;
} else if (e.keyCode == Keyboard.UP) {
upPressed = false;
} else if (e.keyCode == Keyboard.DOWN) {
downPressed = false;
}
}
function loop(e: Event): void {
if (back.hitTestPoint(player.x + leftBumpPoint.x, player.y + leftBumpPoint.y, true)) {
trace("leftBumping");
leftBumping = true;
} else {
leftBumping = false;
}
if (back.hitTestPoint(player.x + rightBumpPoint.x, player.y + rightBumpPoint.y, true)) {
trace("rightBumping");
rightBumping = true;
} else {
rightBumping = false;
}
if (back.hitTestPoint(player.x + upBumpPoint.x, player.y + upBumpPoint.y, true)) {
trace("upBumping");
upBumping = true;
} else {
upBumping = false;
}
if (back.hitTestPoint(player.x + downBumpPoint.x, player.y + downBumpPoint.y, true)) {
trace("downBumping");
downBumping = true;
} else {
downBumping = false;
}
if (back.hitTestPoint(player.x + lowerleftBumpPoint.x, player.y + lowerleftBumpPoint.y, true)) {
trace("lowerrightBumping");
lowerleftBumping = true;
} else {
lowerleftBumping = false;
}
if (back.hitTestPoint(player.x + lowerrightBumpPoint.x, player.y + lowerrightBumpPoint.y, true)) {
trace("lowerrightBumping");
lowerrightBumping = true;
} else {
lowerrightBumping = false;
}
if (leftPressed) {
xSpeed -= speedConstant;
} else if (rightPressed) {
xSpeed += speedConstant;
}
if (leftBumping) {
if (xSpeed < 0) {
xSpeed *= -0.5;
}
}
if (rightBumping) {
if (xSpeed > 0) {
xSpeed *= -0.5;
}
}
if (upBumping) {
if (ySpeed < 0) {
ySpeed *= -0.5;
}
}
if (downBumping) {
if (ySpeed > 0) {
ySpeed = 0; //set the y speed to zero
}
if (upPressed) {
ySpeed = jumpConstant;
}
} else {
ySpeed += gravityConstant;
}
if (xSpeed > maxSpeedConstant) { //moving right
xSpeed = maxSpeedConstant;
} else if (xSpeed < (maxSpeedConstant * -1)) { //moving left
xSpeed = (maxSpeedConstant * -1);
}
xSpeed *= frictionConstant;
ySpeed *= frictionConstant;
if (Math.abs(xSpeed) < 0.5) {
xSpeed = 0;
}
scrollX -= xSpeed;
scrollY -= ySpeed;
back.x = scrollX;
back.y = scrollY;
if (scrollY < -770) {
scrollX = -4;
scrollY = -68;
ySpeed=0
xSpeed=0
}
}
If I was forced to not use the love of my life (Box2D) then I suppose I would play around with hitTestObject and do some math on the angle of any of my slopes to generate the rate at which they would move to start. This question is a bit broad and I think the community here is more for specific problems and not architecture brainstorming.
Related
I have been having a problem getting enemies to appear and this is the final stretch before I can get the game to fully work.
It was supposed to be a one off, the original plan was to have enemies spawning, but the coordinates I have laid out for the enemies have not worked.
I will attach the main code and the enemy .as file, using actionscript3.
// MAIN TIME LINE CODE
import flash.events.Event;
var leftPressed:Boolean = false;
var rightPressed:Boolean = false;
var upPressed:Boolean = false;
var downPressed:Boolean = false;
//buttong pressing
var leftBumping:Boolean = false;
var rightBumping:Boolean = false;
var upBumping:Boolean = false;
var downBumping:Boolean = false;
// collision variables
var leftBumpPoint:Point = new Point(-30, -55);
var rightBumpPoint:Point = new Point(30, -55);
var upBumpPoint:Point = new Point(0, -120);
var downBumpPoint:Point = new Point(0, 0);
// character collision
var gravityConstant:Number = 1.5;
var jumpConstant:Number = - 35;
var maxSpeedConstant:Number = 18;
//jumping
var animationState:String = "idle";
// for the animation
var bulletList:Array = new Array();
var enemyList:Array = new Array();
var currentLevel:int = 1;
var scrollX:Number = 0;
var scrollY:Number = 500;
//parralax
var xSpeed:Number = 0;
var ySpeed:Number = 0;
// speeeeeed
var speedConstant:int = 5;
var friction:Number = 0.9;
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
stage.addEventListener(Event.ENTER_FRAME, loop);
addEnemiesToLevel1();
function addEnemiesToLevel1():void {
addEnemy(340, 390);
/* addEnemy(900, -490);
addEnemy(2005, -115);
addEnemy(1225, -875);*/
//adds an enemy
}
function loop(e:Event):void{
if(back.hitTestPoint(player.x + leftBumpPoint.x, player.y + leftBumpPoint.y, true)){
trace("leftBumping");
leftBumping = true;
} else {
leftBumping = false;
}
if(back.hitTestPoint(player.x + rightBumpPoint.x, player.y + rightBumpPoint.y, true)){
trace("rightBumping");
rightBumping = true;
} else {
rightBumping = false;
}
if(back.hitTestPoint(player.x + upBumpPoint.x, player.y + upBumpPoint.y, true)){
trace("upBumping");
upBumping = true;
} else {
upBumping = false;
}
if(back.hitTestPoint(player.x + downBumpPoint.x, player.y + downBumpPoint.y, true)){
trace("downBumping");
downBumping = true;
} else {
downBumping = false;
}
if(leftPressed){
xSpeed -= speedConstant;
/*player.x -= xSpeed;*/
player.scaleX = -1;
trace("fff");
} else if(rightPressed) {
xSpeed += speedConstant;
/*player.x += xSpeed;*/
player.scaleX = 1;
// player movement left and right
}
/*player.scaleX = 1;*/ // copy in case
/*if(upPressed){
ySpeed -= speedConstant;
} else if(downPressed){
ySpeed += speedConstant;
}*/
if(leftBumping){
if(xSpeed < 0){
xSpeed *= -0.5;
}
}
if(rightBumping){
if(xSpeed > 0){
xSpeed *= -0.5;
}
}
if(upBumping){
if(ySpeed < 0){
ySpeed *= -0.5;
}
} ////BUMPING
if(downBumping){ //if touching the floor
if(ySpeed > 0){
ySpeed = 0;
trace("1")
}
if(upPressed) { //y speed to jump constant
ySpeed = jumpConstant;
trace("2");
}
} else { // if not touching the floor
ySpeed += gravityConstant; // moves downward
trace("portobello");
}
if(xSpeed > maxSpeedConstant) { // lettin you move right
xSpeed = maxSpeedConstant;
} else if(xSpeed < (maxSpeedConstant * -1)) { // leting you move left
xSpeed = (maxSpeedConstant * -1);
}
xSpeed *= friction;
ySpeed *= friction;
if(Math.abs(xSpeed) < 0.5) {
xSpeed = 0;
}
scrollX -= xSpeed;
scrollY -= ySpeed;
back.x = scrollX;
back.y = scrollY;
if( ( leftPressed || rightPressed || xSpeed > speedConstant || xSpeed < speedConstant *-1 ) && downBumping) {
animationState = "running";
} else if(downBumping) {
animationState = "idle";
} else {
animationState = "jumping";
}
if (player.currentLabel != animationState) {
player.gotoAndStop(animationState);
}
}
if (player.currentLabel != animationState) {
player.gotoAndStop(animationState);
}
if (enemyList.length > 0) // if there are any enemies left in the enemyList
{
for (var i:int = 0; i < enemyList.length; i++) // for each enemy in the enemyList
{
if (bulletList.length > 0) // if there are any bullets alive
{
for (var j:int = 0; j < bulletList.length; j++) // for each bullet in the bulletList
{
if ( enemyList[i].hitTestObject(bulletList[j]) )
{
trace("Bullet and Enemy are colliding");
enemyList[i].removeSelf();
bulletList[j].removeSelf();
}
}
}
}
}
function keyDownHandler(e:KeyboardEvent):void{
if(e.keyCode == Keyboard.LEFT){
leftPressed = true;
} else if(e.keyCode == Keyboard.RIGHT){
rightPressed = true;
} else if(e.keyCode == Keyboard.UP){
upPressed = true;
} else if(e.keyCode == Keyboard.DOWN){
downPressed = true;
}
}
function keyUpHandler(e:KeyboardEvent):void{
if(e.keyCode == Keyboard.LEFT){
leftPressed = false;
} else if(e.keyCode == Keyboard.RIGHT){
rightPressed = false;
} else if(e.keyCode == Keyboard.UP){
upPressed = false;
} else if(e.keyCode == Keyboard.DOWN){
downPressed = false;
}
if(e.keyCode == Keyboard.SPACE) {
fireBullet();
}
}
function addEnemy(xLocation:int, yLocation:int): void {
var enemy:Enemy = new Enemy(xLocation, yLocation);
back.addChild(enemy);
enemy.addEventListener(Event.REMOVED, enemyRemoved);
enemyList.push(enemy);
}
function enemyRemoved(e:Event):void {
e.currentTarget.removeEventListener(Event.REMOVED, enemyRemoved);
enemyList.splice(enemyList.indexOf(e.currentTarget), 1);
}
function fireBullet():void {
var playerDirection:String;
if(player.scaleX < 0) {
playerDirection = "left";
} else if(player.scaleX > 0) {
playerDirection = "right";
}
var bullet:Bullet = new Bullet(player.x, player.y, playerDirection);
stage.addChild(bullet);
// firing direction
}
function bulletRemoved(e:Event):void
{
e.currentTarget.removeEventListener(Event.REMOVED, bulletRemoved); // stops and error from occuring in the event listener
bulletList.splice(bulletList.indexOf(e.currentTarget), 1); // removes a bullet from the list
}
The enemy .as file:
package {
import flash.display.MovieClip;
import flash.events.Event;
public class Enemy extends MovieClip {
public function Enemy(xLocation:int, yLocation:int) {
// constructor code
x = xLocation;
y = yLocation;
addEventListener(Event.ENTER_FRAME, loop);
}
public function loop(e:Event):void {
//looping code
}
public function removeSelf():void {
trace("remove self");
removeEventListener(Event.ENTER_FRAME, loop); //stops the loop
this.parent.removeChild(this); // tell this objects "parent object" to remove said object
}
}
}
I am trying to follow a tutorial to help me learn how to create a platform game. However, whenever I test my code my player will go through the background(named back instead of background). I have tried to add more points to stop it from doing this but it just continues. Anyone experienced in hitTestPoint or flash please assist me!
Look at the Bumping part of my code
http://as3gametuts.com/2012/01/08/platformer-3/ --- use this.
The background is just the platform where the player stands. The hitTestPoint code should stop the player falling through the platform or going through walls. Sometimes hitTestPoint will work when the player is going very slow.
import flash.events.KeyboardEvent;
import flash.geom.Point;
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
stage.addEventListener(Event.ENTER_FRAME, loop);
var leftPressed:Boolean = false;
var rightPressed:Boolean = false;
var upPressed:Boolean = false;
var downPressed:Boolean = false;
var leftReleased:Boolean = false;
var rightReleased:Boolean = false;
var upReleased:Boolean = false;
var downReleased:Boolean = false;
var xSpeed:int = 10;
var ySpeed:int = 10;
var scrollX:int = 0;
var scrollY:int = 0;
var speedConstant:int = 5;
var friction:Number = 0.95;
var leftBumping:Boolean = false;
var rightBumping:Boolean = false;
var upBumping:Boolean = false;
var downBumping:Boolean = false;
var upleftBumping:Boolean = false;
var uprightBumping:Boolean = false;
var downrightBumping:Boolean = false;
var downleftBumping:Boolean = false;
var leftBumpPoint:Point = new Point(-30, -55)
var upleftBumpPoint:Point = new Point(-30, -87)
var rightBumpPoint:Point = new Point(30, -55)
var uprightBumpPoint:Point = new Point(30, -87)
var upBumpPoint:Point = new Point(0, -120)
var downBumpPoint:Point = new Point(0, 0)
var downrightBumpPoint:Point = new Point(30, -27)
var downleftBumpPoint:Point = new Point(-30, -27)
function keyDownHandler(e:KeyboardEvent):void{
if(e.keyCode == Keyboard.LEFT){
leftPressed = true;
} else if(e.keyCode == Keyboard.RIGHT) {
rightPressed = true;
} else if(e.keyCode == Keyboard.UP) {
upPressed = true;
} else if(e.keyCode == Keyboard.DOWN) {
downPressed = true;
}
}
function keyUpHandler(e:KeyboardEvent):void{
if(e.keyCode == Keyboard.LEFT){
leftPressed = false;
} else if(e.keyCode == Keyboard.RIGHT){
rightPressed = false;
} else if(e.keyCode == Keyboard.UP){
upPressed = false;
} else if(e.keyCode == Keyboard.DOWN){
downPressed = false;
}
}
/*
function keyUpHandler(e:KeyboardEvent):void{
if(e.keyCode == Keyboard.LEFT){
leftReleased = true;
} else if(e.keyCode == Keyboard.RIGHT){
rightReleased = true;
} else if(e.keyCode == Keyboard.UP){
upReleased = true;
} else if(e.keyCode == Keyboard.DOWN){
downReleased = true;
}
}*/
function loop(e:Event):void{
if(back.hitTestPoint(player.x + leftBumpPoint.x, player.y + leftBumpPoint.y, true)){
trace("leftBumping");
leftBumping = true;
} else {
leftBumping = false;
}
if(back.hitTestPoint(player.x + rightBumpPoint.x, player.y + rightBumpPoint.y, true)){
trace("rightBumping");
rightBumping = true;
} else {
rightBumping = false;
}
if(back.hitTestPoint(player.x + upBumpPoint.x, player.y + upBumpPoint.y, true)){
trace("upBumping")
upBumping = true;
} else {
upBumping = false;
}
if(back.hitTestPoint(player.x + downBumpPoint.x, player.y + downBumpPoint.y, true)){
trace("downBumping")
downBumping = true;
} else {
downBumping = false;
}
if(back.hitTestPoint(player.x + upleftBumpPoint.x, player.y + upleftBumpPoint.y, true)){
trace("upleftBumping")
upleftBumping = true;
} else {
upleftBumping = false;
}
if(back.hitTestPoint(player.x + uprightBumpPoint.x, player.y + uprightBumpPoint.y, true)){
trace("uprightBumping")
uprightBumping = true;
} else {
uprightBumping = false;
}
if(back.hitTestPoint(player.x + downrightBumpPoint.x, player.y + downrightBumpPoint.y, true)){
trace("downrightBumping")
downrightBumping = true;
} else {
downrightBumping = false;
}
if(back.hitTestPoint(player.x + downleftBumpPoint.x, player.y + downleftBumpPoint.y, true)){
trace("downleftBumping")
downleftBumping = true;
} else {
downleftBumping = false;
}
if(leftPressed){
xSpeed -= speedConstant ;
} else if(rightPressed){
xSpeed += speedConstant;
} else if(upPressed){
ySpeed += speedConstant;
} else if(downPressed){
ySpeed -= speedConstant;
}
if(leftBumping){
if(xSpeed < 0){
xSpeed *= -0.5;
}
}
if(rightBumping){
if(xSpeed > 0){
xSpeed *= -0.5;
}
}
if(upBumping){
if(ySpeed < 0){
ySpeed *= -0.5;
}
}
if(downBumping){
if(ySpeed > 0){
ySpeed *= -0.5;
}
}
xSpeed *= friction;
ySpeed *= friction;
scrollX -= xSpeed;
scrollY += ySpeed;
back.x = scrollX;
back.y = scrollY;
}
I didn't analyzed whole your code but it just seems like typical bullet scenario.
If you testing only one point against an obstacle which width is less then player speed you will occasionally miss the obstacle because in one frame the test point is on one side of it and in next frame on the other - point is not hitting it on both frames.
The easiest way to fix it is just to make sure your obstacles have width greater than speed of your player.
Another way is to test intermediate points between positions of your test points. You can do this easly through Point.interplate() method and additionally you can also adjust number of intermediate tests according to your player speed and size of obsticles.
But since you started with Box2D, you can just set a bullet flag in objects physics properties.
I am working on a basic flash platformer game in Adobe Flash using ActionScript 3. I can't seem to get my character to start above the ground in the stage I made. I was able to move my character up into the stage, but after adding gravity and jump mechanics it can't move that high anymore so I am stuck now. I am following this tutorial but I created my own background and player. Any help is greatly appreciated.
here is the code:
import flash.events.KeyboardEvent;
//Variables
var leftPressed:Boolean = false;
var rightPressed:Boolean = false;
var upPressed:Boolean = false;
var downPressed:Boolean = false;
var xSpeed:Number = 0;
var ySpeed:Number = 0;
//variables to keep track of background x and y locations
var scrollX:Number = 0;
var scrollY:Number = 0;
//Constant Variables
var speedConstant:int = 5;
var friction:Number = 0.95;
var jumpConstant:Number = -25;
//Gravity constant
var gravityConstant:Number = 1.5;
//Variables for collission detection
var leftBumping:Boolean = false;
var rightBumping:Boolean = false;
var upBumping:Boolean = false;
var downBumping:Boolean = false;
var leftBumpPoint:Point = new Point(-30, -55);
var rightBumpPoint:Point = new Point(30, -55);
var upBumpPoint:Point = new Point(0, -120);
var downBumpPoint:Point = new Point(0, 0);
//Add listeners
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
stage.addEventListener(Event.ENTER_FRAME, loop);
//Functions for listners
function keyDownHandler(e:KeyboardEvent):void{
if(e.keyCode == Keyboard.LEFT){
leftPressed = true;
}
else if(e.keyCode == Keyboard.RIGHT){
rightPressed = true;
}
else if(e.keyCode == Keyboard.UP){
upPressed = true;
}
else if(e.keyCode == Keyboard.DOWN){
downPressed = true;
}
}
function keyUpHandler(e:KeyboardEvent):void{
if(e.keyCode == Keyboard.LEFT){
leftPressed = false;
}
else if(e.keyCode == Keyboard.RIGHT){
rightPressed = false;
}
else if(e.keyCode == Keyboard.UP){
upPressed = false;
}
else if(e.keyCode == Keyboard.DOWN){
downPressed = false;
}
}
//Main Game Loop
function loop(e:Event):void{
if(back.hitTestPoint(player.x + leftBumpPoint.x, player.y + leftBumpPoint.y, true)){
trace("leftBumping");
leftBumping = true;
} else {
leftBumping = false;
}
if(back.hitTestPoint(player.x + rightBumpPoint.x, player.y + rightBumpPoint.y, true)){
trace("rightBumping");
rightBumping = true;
} else {
rightBumping = false;
}
if(back.hitTestPoint(player.x + upBumpPoint.x, player.y + upBumpPoint.y, true)){
trace("upBumping");
upBumping = true;
} else {
upBumping = false;
}
if(back.hitTestPoint(player.x + downBumpPoint.x, player.y + downBumpPoint.y, true)){
trace("downBumping");
downBumping = true;
} else {
downBumping = false;
}
if(leftPressed){
xSpeed -= speedConstant;
} else if(rightPressed){
xSpeed += speedConstant;
}
/* if(upPressed){
ySpeed -= speedConstant;
} else if(downPressed){
ySpeed += speedConstant;
}*/
if(leftBumping){
if(xSpeed < 0){
xSpeed *= -0.5;
}
}
if(rightBumping){
if(xSpeed > 0){
xSpeed *= -0.5;
}
}
if(upBumping){
if(ySpeed < 0){
ySpeed *= -0.5;
}
}
if(downBumping){ //if we are touching the floor
if(ySpeed > 0){
ySpeed *= -0.5;
}
if(upPressed){ //and if the up arrow is pressed
ySpeed = jumpConstant; //set the y speed to the jump constant
}
}
else { //if we are not touching the floor
ySpeed += gravityConstant; //accelerate downwards
}
xSpeed *= friction;
ySpeed *= friction;
scrollX -= xSpeed;
scrollY -= ySpeed;
back.x = scrollX;
back.y = scrollY;
}
Wellp, I've looked everywhere, and I'm as green as they come at this..
I've recently started playing with AS3 because I want to learn it, so I began building a simple platformer deal.
I think I know what my problem is, but finding the solution has been hades and a half.
http://sta.sh/0ucxs1xjaua
From what I can research, this is a matter of player position calculation/framerates. The character has gone too far before the program calculates his position as in relation to the floor, and when it finally recognizes where he is, it begins recalibrating, bringing him back to Zero.
My particular problem is that everyone who makes tutorials and stuff uses different labeling systems and I can't find anything well enough adapted to this particular problem in plainspeak.
So please forgive me for asking, but is there any chance someone could check out my code and help me spot where things are going wrong. I feel like it has something to do with the
if(downBumping) bit, but I'm struggling to figure out what to change and how. :\
Here's the code:
import flash.geom.Point;
import flash.events.KeyboardEvent;
import flash.events.Event;
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
stage.addEventListener(Event.ENTER_FRAME, loop);
var scrollX:Number = 0;
var scrollY:Number = 0;
var xSpeed:Number = 0;
var ySpeed:Number = 0;
var leftPressed:Boolean = false;
var rightPressed:Boolean = false;
var upPressed:Boolean = false;
var downPressed:Boolean = false;
var leftBumping:Boolean = false;
var rightBumping:Boolean = false;
var upBumping:Boolean = false;
var downBumping:Boolean = false;
var leftBumpPoint:Point = new Point(-62, 40);
var rightBumpPoint:Point = new Point(62, 40);
var upBumpPoint:Point = new Point(0, -83);
var downBumpPoint:Point = new Point(0, 80);
var animationState:String = "idle";
class Player extends player {
var speedConstant:int = 4;
var friction:Number = .75;
var maxSpeedConstant:Number = 18;
var gravityConstant:Number = 16;
var jumpConstant:Number = -180;
}
function loop(e:Event):void{
if(back.collisions.hitTestPoint(player.x + leftBumpPoint.x, player.y + leftBumpPoint.y, true)){
trace("my butt!!");
leftBumping = true;
} else {
leftBumping = false;
}
if(back.collisions.hitTestPoint(player.x + rightBumpPoint.x, player.y + rightBumpPoint.y, true)){
trace("my face!!");
rightBumping = true;
} else {
rightBumping = false;
}
if(back.collisions.hitTestPoint(player.x + upBumpPoint.x, player.y + upBumpPoint.y, true)){
trace("my head!!");
upBumping = true;
} else {
upBumping = false;
}
if(back.collisions.hitTestPoint(player.x + downBumpPoint.x, player.y + downBumpPoint.y, true)){
trace("my feet!");
downBumping = true;
} else {
downBumping = false;
}
//speed mechanics
if(leftPressed){
xSpeed -= speedConstant;
player.scaleX = -1;
} else if(rightPressed){
xSpeed += speedConstant;
player.scaleX = 1;
}
if(leftBumping){
if(xSpeed < 0){
xSpeed *= -0.5;
}
}
if(rightBumping){
if(xSpeed > 0){
xSpeed *= -0.5;
}
}
if(upBumping){
if(ySpeed < 0){
ySpeed *= -0.5;
}
}
if(downBumping){
if(ySpeed > 0){
ySpeed *= -.19;
}
if(upPressed){ //and if we're pressing UP
ySpeed = jumpConstant; //set y speed to jump constant
}
} else {//if we are not touching the floor
ySpeed += gravityConstant; //accelerate downwards
}
if(xSpeed > maxSpeedConstant){
xSpeed = maxSpeedConstant;
} else if(xSpeed < (maxSpeedConstant * -1)){
xSpeed = (maxSpeedConstant * -1);
}
xSpeed *= friction;
ySpeed *= friction;
if(Math.abs(xSpeed) < 0.5){
xSpeed = 0;
}
scrollX -= xSpeed;
scrollY -= ySpeed;
back.x = scrollX;
back.y = scrollY;
if( ( leftPressed || rightPressed || xSpeed > speedConstant || xSpeed < speedConstant *-1) && downBumping){
animationState = "walk";
}else if(downBumping){
animationState = "idle";
}
if(player.currentLabel !=animationState){
player.gotoAndStop(animationState)
}
}
function keyDownHandler(e:KeyboardEvent):void{
if(e.keyCode == Keyboard.LEFT){
leftPressed = true;
} else if(e.keyCode == Keyboard.RIGHT){
rightPressed = true;
} else if(e.keyCode == Keyboard.UP){
upPressed = true;
} else if(e.keyCode == Keyboard.DOWN){
downPressed = true;
}
}
function keyUpHandler(e:KeyboardEvent):void{
if(e.keyCode == Keyboard.LEFT){
leftPressed = false;
} else if(e.keyCode == Keyboard.RIGHT){
rightPressed = false;
} else if(e.keyCode == Keyboard.UP){
upPressed = false;
} else if(e.keyCode == Keyboard.DOWN){
downPressed = false;
}
}
Any help would be so very appreciated. I want to learn, but it's hard when I don't even know what exactly to look for.. Thanks so much!
I'm going to write you what you need to change for a little solution. But that's not the best effort. I just don't wanted to change your whole logic, but you can use this in your logic.
don't use a time based calculation for fixing y position after a jump,
let that calculation be instant
if your player's y position get's below of zero point, set it's y position to zero and stop accelerating down
Also, an important point, don't use your hittest functions first, use them after your acceleration functions, in every loop, that makes better effort.
Also, increasing framerate of your movie and decreasing acceleration values is great way to make things work so much better.
I was trying to code the code below into a package that came from a tutorial, but it originally had it in the timeline
now it gives the error 1180: Call to a possibly undefined method Player.
here is a snippet from the concerning code (full code is below that)
private var player:Player;
public function RamboCat()
{
player = new Player();
So Player is not defined. Does this mean it's missing a AS file or something
But i'm trying to tell flash to use a image (image is instanced 'player')
i added the above code (minus the public function that already existed) because i found
a source which explained i should add those: http://www.actionscript.org/forums/showthread.php3?t=268065
my experience with "package" and "import" things is a bit low. So hopefully you can help.
package
{
import flash.display.MovieClip;
import flash.events.*;
import flash.geom.Point;
public class RamboCat extends MovieClip
{
var leftPressed:Boolean = false;
var rightPressed:Boolean = false;
var upPressed:Boolean = false;
var downPressed:Boolean = false;
var leftBumping:Boolean = false;
var rightBumping:Boolean = false;
var upBumping:Boolean = false;
var downBumping:Boolean = false;
var leftBumpPoint:Point = new Point(-30, -55);
var rightBumpPoint:Point = new Point(30, -55);
var upBumpPoint:Point = new Point(0, -120);
var downBumpPoint:Point = new Point(0, 0);
var scrollX:Number = 0;
var scrollY:Number = 500;
var xSpeed:Number = 0;
var ySpeed:Number = 0;
var speedConstant:Number = 4;
var frictionConstant:Number = 0.9;
var gravityConstant:Number = 1.8;
var jumpConstant:Number = -35;
var maxSpeedConstant:Number = 18;
var doubleJumpReady:Boolean = false;
var upReleasedInAir:Boolean = false;
var keyCollected:Boolean = false;
var doorOpen:Boolean = false;
var currentLevel:int = 1;
var animationState:String = "idle";
var bulletList:Array = new Array();
var enemyList:Array = new Array();
var bumperList:Array = new Array();
private var player:Player;
public function RamboCat()
{
player = new Player();
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
stage.addEventListener(Event.ENTER_FRAME, loop);
addEnemiesToLevel1();
addBumpersToLevel1();
}
public function addEnemiesToLevel1():void
{
addEnemy(620, -115);
addEnemy(900, -490);
addEnemy(2005, -115);
addEnemy(1225, -875);
}
public function addBumpersToLevel1():void
{
addBumper(500, -115);
addBumper(740, -115);
}
public function loop(e:Event):void{
if(back.collisions.hitTestPoint(player.x + leftBumpPoint.x, player.y + leftBumpPoint.y, true)){
//trace("leftBumping");
leftBumping = true;
} else {
leftBumping = false;
}
if(back.collisions.hitTestPoint(player.x + rightBumpPoint.x, player.y + rightBumpPoint.y, true)){
//trace("rightBumping");
rightBumping = true;
} else {
rightBumping = false;
}
if(back.collisions.hitTestPoint(player.x + upBumpPoint.x, player.y + upBumpPoint.y, true)){
//trace("upBumping");
upBumping = true;
} else {
upBumping = false;
}
if(back.collisions.hitTestPoint(player.x + downBumpPoint.x, player.y + downBumpPoint.y, true)){
//trace("downBumping");
downBumping = true;
} else {
downBumping = false;
}
if(leftPressed){
xSpeed -= speedConstant;
player.scaleX = -1;
} else if(rightPressed){
xSpeed += speedConstant;
player.scaleX = 1;
}
/*if(upPressed){
ySpeed -= speedConstant;
} else if(downPressed){
ySpeed += speedConstant;
}*/
if(leftBumping){
if(xSpeed < 0){
xSpeed *= -0.5;
}
}
if(rightBumping){
if(xSpeed > 0){
xSpeed *= -0.5;
}
}
if(upBumping){
if(ySpeed < 0){
ySpeed *= -0.5;
}
}
if(downBumping){ //if we are touching the floor
if(ySpeed > 0){
ySpeed = 0; //set the y speed to zero
}
if(upPressed){ //and if the up arrow is pressed
ySpeed = jumpConstant; //set the y speed to the jump constant
}
//DOUBLE JUMP
if(upReleasedInAir == true){
upReleasedInAir = false;
}
if(doubleJumpReady == false){
doubleJumpReady = true;
}
} else { //if we are not touching the floor
ySpeed += gravityConstant; //accelerate downwards
//DOUBLE JUMP
if(upPressed == false && upReleasedInAir == false){
upReleasedInAir = true;
//trace("upReleasedInAir");
}
if(doubleJumpReady && upReleasedInAir){
if(upPressed){ //and if the up arrow is pressed
//trace("doubleJump!");
doubleJumpReady = false;
ySpeed = jumpConstant; //set the y speed to the jump constant
}
}
}
if(keyCollected == false){
if(player.hitTestObject(back.other.doorKey)){
back.other.doorKey.visible = false;
keyCollected = true;
trace("key collected");
}
}
if(doorOpen == false){
if(keyCollected == true){
if(player.hitTestObject(back.other.lockedDoor)){
back.other.lockedDoor.gotoAndStop(2);
doorOpen = true;
trace("door open");
}
}
}
if(xSpeed > maxSpeedConstant){ //moving right
xSpeed = maxSpeedConstant;
} else if(xSpeed < (maxSpeedConstant * -1)){ //moving left
xSpeed = (maxSpeedConstant * -1);
}
xSpeed *= frictionConstant;
ySpeed *= frictionConstant;
if(Math.abs(xSpeed) < 0.5){
xSpeed = 0;
}
scrollX -= xSpeed;
scrollY -= ySpeed;
back.x = scrollX;
back.y = scrollY;
sky.x = scrollX * 0.2;
sky.y = scrollY * 0.2;
if( ( leftPressed || rightPressed || xSpeed > speedConstant || xSpeed < speedConstant *-1 ) && downBumping){
animationState = "running";
} else if(downBumping){
animationState = "idle";
} else {
animationState = "jumping";
}
if(player.currentLabel != animationState){
player.gotoAndStop(animationState);
}
if (enemyList.length > 0) // if there are any enemies left in the enemyList
{
for (var i:int = 0; i < enemyList.length; i++) // for each enemy in the enemyList
{
if (bulletList.length > 0) // if there are any bullets alive
{
for (var j:int = 0; j < bulletList.length; j++) // for each bullet in the bulletList
{
if ( enemyList[i].hitTestObject(bulletList[j]) )
{
trace("Bullet and Enemy are colliding");
enemyList[i].removeSelf();
bulletList[j].removeSelf();
}
// enemyList[i] will give you the current enemy
// bulletList[j] will give you the current bullet
// this will check all combinations of bullets and enemies
// and see if any are colliding
}
}
}
}
//corralling the bad guys with bumpers
if (enemyList.length > 0){ //enemies left in the enemyList?
for (var k:int = 0; k < enemyList.length; k++){ // for each enemy in the enemyList
if (bumperList.length > 0){
for (var h:int = 0; h < bumperList.length; h++){ // for each bumper in the List
if ( enemyList[k].hitTestObject(bumperList[h]) ){
enemyList[k].changeDirection();
}
}
}
}
}
//player and enemy collisions
if (enemyList.length > 0){ //enemies left?
for (var m:int = 0; m < enemyList.length; m++){ // for each enemy in the enemyList
if ( enemyList[m].hitTestObject(player) ){
trace("player collided with enemy");
//code to damage player goes here, maybe integrate with a health bar?
enemyList[m].removeSelf();
}
}
}
}
public function nextLevel():void{
currentLevel++;
trace("Next Level: " + currentLevel);
if(currentLevel == 2){
gotoLevel2();
}
// can be extended...
// else if(currentLevel == 3) { gotoLevel3(); } // etc, etc.
}
public function gotoLevel2():void{
back.other.gotoAndStop(2);
back.visuals.gotoAndStop(2);
back.collisions.gotoAndStop(2);
scrollX = 0;
scrollY = 500;
keyCollected = false;
back.other.doorKey.visible = true;
doorOpen = false;
back.other.lockedDoor.gotoAndStop(1);
}
public function keyDownHandler(e:KeyboardEvent):void{
if(e.keyCode == Keyboard.LEFT){
leftPressed = true;
} else if(e.keyCode == Keyboard.RIGHT){
rightPressed = true;
} else if(e.keyCode == Keyboard.UP){
upPressed = true;
} else if(e.keyCode == Keyboard.DOWN){
downPressed = true;
if(doorOpen && player.hitTestObject(back.other.lockedDoor)){
//proceed to the next level if the player is touching an open door
nextLevel();
}
}
}
public function keyUpHandler(e:KeyboardEvent):void{
if(e.keyCode == Keyboard.LEFT){
leftPressed = false;
} else if(e.keyCode == Keyboard.RIGHT){
rightPressed = false;
} else if(e.keyCode == Keyboard.UP){
upPressed = false;
} else if(e.keyCode == Keyboard.DOWN){
downPressed = false;
}
if(e.keyCode == Keyboard.SPACE){
fireBullet();
}
}
public function fireBullet():void
{
var playerDirection:String;
if(player.scaleX < 0){
playerDirection = "left";
} else if(player.scaleX > 0){
playerDirection = "right";
}
var bullet:Bullet = new Bullet(player.x - scrollX, player.y - scrollY, playerDirection, xSpeed);
back.addChild(bullet);
bullet.addEventListener(Event.REMOVED, bulletRemoved);
bulletList.push(bullet);
}
public function bulletRemoved(e:Event):void
{
e.currentTarget.removeEventListener(Event.REMOVED, bulletRemoved); //this just removes the eventListener so we don't get an error
bulletList.splice(bulletList.indexOf(e.currentTarget), 1);
//this removes 1 object from the bulletList, at the index of whatever object caused this function to activate
}
public function addEnemy(xLocation:int, yLocation:int):void
{
var enemy:Enemy = new Enemy(xLocation, yLocation);
back.addChild(enemy);
enemy.addEventListener(Event.REMOVED, enemyRemoved);
enemyList.push(enemy);
}
public function addBumper(xLocation:int, yLocation:int):void
{
var bumper:Bumper = new Bumper(xLocation, yLocation);
back.addChild(bumper);
bumper.visible = false;
bumperList.push(bumper);
}
public function enemyRemoved(e:Event):void
{
e.currentTarget.removeEventListener(Event.REMOVED, enemyRemoved);
//this just removes the eventListener so it doesn't give an error
enemyList.splice(enemyList.indexOf(e.currentTarget), 1); //this removes 1 object from the enemyList, at the index of whatever object caused this function to activate
}
}
}
It cannot be a reference to an instance of player as: new Player(); creates a new instance of Player.
So Player needs to be a class. If you are using Adobe Flash Professional, you can right click the image in the library, click properties, actionscript tab, then export to actionscript (make sure the class is called Player). this will create the class for the image for you.
If you are not using Adobe flash professional, then let me know and I will help with the IDE you are using.
Edit: And looking back you will also need to import Keyboard Class if you have not done already.
Looks like you need an import statement for Player.
To import a single class:
import your.package.name.ClassName;
To import every class in a package:
import your.package.name.*;
So you need:
import xxx.Player;
where xxx is the package where Player class sits.