PDA

View Full Version : side scrolling game


johosher
06-06-2008, 06:07 PM
im trying to make a side scrolling game and i was wondering what is the best way for the "character" to tell where the bounds of the level are. i have a boundry layer and a character layer but i am having some complications... if anyone knows any good tutorials or can help me out let me know!

johosher
06-06-2008, 06:50 PM
and here is my code:


package
{
import flash.display.*;
import flash.events.*;
import flash.geom.*;
import flash.text.*;
import flash.display.BitmapData;
import flash.geom.Rectangle;
import flash.geom.Point;
import flash.display.Stage;

public class bounceYourButt extends Sprite
{
public var BALL:Ball = new Ball(20, Math.random() * 0xffffff);


public var xspeed:Number = 0;
public var yspeed:Number = 0;
public var angle:Number = 0;


public var topspeed:Number = 15;
public var neg_topspeed:Number = -15;

public var gravity:Number = 0.86;
public var bounce:Number = -0.7;
public var power = 3;
public var friction = 0.8;

public static var stage:Stage;
public var _stage:Stage;
public var LAYERS:layers = new layers();

public var bound:boundry = new boundry();
public var SLOPE:slope = new slope();



public var landed:Boolean;


public function bounceYourButt(stageRef:Stage)
{
addEventListener(Event.ADDED_TO_STAGE,stageadded);
this.addEventListener(Event.ENTER_FRAME ,Draw);
// .addEventListener(KeyboardEvent.KEY_DOWN, keyHandler);

/*square.graphics.beginFill(0xFF);
square.graphics.drawRect(40, 400, 400, 10);
square.graphics.endFill();
this.LAYERS.layer_BACKGROUND.addChild(square);*/
bound.x = 50;
bound.y = 450;
/*SLOPE.x = 500;
SLOPE.y = 450;
this.LAYERS.layer_BACKGROUND.addChild(SLOPE);*/

this.addChild(LAYERS);
BALL.x = 100;
BALL.y = 100;
this.addChild(BALL);


this.LAYERS.layer_BACKGROUND.addChild(bound);
}

private function stageadded(evt:Event)
{
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyHandler);
}

public function Draw(e:Event):void
{
xspeed *= friction;
yspeed += gravity;
var botWall:Number = stage.stageHeight;
var point:Number;

if(this.BALL.x < stage.stageWidth/2)
this.BALL.x = this.BALL.x + xspeed;
else
this.LAYERS.x -= xspeed;

this.BALL.y = this.BALL.y + yspeed;

this.BALL.rotation += xspeed;
angle = Math.atan(yspeed/xspeed)/(Math.PI/180);

if (xspeed<0)
angle += 180;

if (xspeed>=0 && yspeed<0)
angle += 360;

//this.BALL.rotation = angle- this.BALL.rotation;

if(BALL.hitTestObject(bound)) //BALL.y + BALL.radius >= botWall)
{
point = BALL.y ;
BALL.y = bound.y - BALL.radius;
yspeed *= bounce;
}

if(BALL.y + BALL.radius == bound.y)
landed = true;
else
landed = false;

if(this.BALL.x >= 600 || this.BALL.x <= 0 || this.BALL.y >= 500 )//|| this.BALL.y <= 0)
{
this.BALL.x = 100;
this.BALL.y = 20;
xspeed = 0;
yspeed = 0;
gravity = 1.5;
}

}

public function keyHandler(evt:KeyboardEvent):void
{
if(evt.keyCode == 37)//left
{
if(xspeed >= neg_topspeed)
xspeed -= power;
}
if(evt.keyCode == 39)//right
{
if(xspeed <= topspeed)
xspeed += power;
}
if(evt.keyCode == 38)//up
{
if(landed == true)
{
if(yspeed >= neg_topspeed)
yspeed -= 30;
}

}

if(evt.keyCode == 40)//down
{
if(yspeed <= 30)
yspeed += power;
}


if(evt.keyCode == 38 && evt.keyCode == 39)
{
if(xspeed <= topspeed)
xspeed += power;

if(landed == true)
{
if(yspeed >= neg_topspeed)
yspeed -= 15;
}
}

}
}
}

GMaker0507
06-07-2008, 02:49 PM
Have you tried the getBounds function.

DisplayObject.getBounds(targetCoordinateSpace:Disp layObject):Rectangle

It returns a rectangle, so import 'flash.geom.Rectangle'

the x value of the rectangle is lef boundrary, the y is the top, and then their is the width and height.


uhh its not related to your question, but a tip for handling keyboard input.
NOTE - You need to import 'flash.ui.Keyboard'

Create a class level array to hold all the keys
public var Keys:Array = new Array(256)

Then add a listener for they are pressed

public function class()
{
stage.addEventListener(KeyboardEvent.KEY_DOWN,OnKe yDown)
stage.addEventListener(KeyboardEvent.KEY_UP,OnKeyU p)
}
public function OnKeyDown(e:KeyboardEvent)
{
Keys[e.keyCode]=true
}
public function OnKeyUp(e:KeyboardEvent)
{
Keys[e.keyCode]=false
}

Then,in your OnEnterFrame code you can
if(Keys[Keyboard.RIGHT]){}

johosher
06-09-2008, 04:34 PM
thanks gmaker but im still having problems! since the getbounds returns a rectangle, what if i have a slope? and do i just do a character.hitTestPoint on the height of the rectangle?

johosher
06-09-2008, 04:40 PM
and i want to a hit test on pixels and not the bounding box if that makes since

GMaker0507
06-09-2008, 09:50 PM
I get that you dont want to hitTest against bounding boxes,but dont fully get what your saying about the slope

NickZA
06-11-2008, 11:55 AM
johosher

(See attached file for diagram)

If I get your question about the slope correctly... if your character's arms are wider than his body then when he approaches the slope, the bottom corner of the bounding rectanlge will appear below his hand, meaning that when he mounts the slope, he will appear to hover off the surface of the slope.

What you need to do then is to use a narrower bounding box for his feet, or even a single point representing his feet's "collision" with the surface of the slope.

-Nick