Skip to main content
September 6, 2011
Question

Sidescroller help?

  • September 6, 2011
  • 3 replies
  • 1263 views

Hi, People

Im Currently making a side scroller game for IOS and im using this peice of code on my main timeline..

<AS>

import flash.events.*;

import flash.utils.*;

import flash.display.MovieClip;

import flash.ui.Multitouch;

import flash.ui.MultitouchInputMode;

import Classes.GlobalVaribles;

Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;

stop();

var vy:Number;

var vx:Number;

var inAir:Boolean = false;

MoneyText.text = "£: " + GlobalVaribles.Money;

// set Default Values

vy = 0;

vx = 0;

Player.gotoAndStop(1);

// stage focus;

stage.focus = stage;

// add event listeners

this.addEventListener(Event.ENTER_FRAME,enterFrameHandler);

// Movement Event Listeners;

Attack_btn.addEventListener(TouchEvent.TOUCH_BEGIN,AttackBtnBegin);

Attack_btn.addEventListener(TouchEvent.TOUCH_END,AttackBtnEnd);

JumpBtn.addEventListener(TouchEvent.TOUCH_TAP,JumpBtnHandler);

Change_DirectionRight.addEventListener(TouchEvent.TOUCH_BEGIN, RightBtnBegin);

Change_DirectionRight.addEventListener(TouchEvent.TOUCH_END, RightBtnEnd);

Change_DirectionLeft.addEventListener(TouchEvent.TOUCH_BEGIN, LeftBtnBegin);

Change_DirectionLeft.addEventListener(TouchEvent.TOUCH_END, LeftBtnEnd);

function enterFrameHandler(e:Event):void

{

// Apply Gravity on Player

vy +=  2;

// move player

Player.x +=  vx;

Player.y +=  vy;

// process collisions

processCollisions();

//scroll the stage

scrollStage();

//Check if player in air

if (! Player.hitTestObject(Boundries))

{

inAir = true;

}

if (Player.hitTestObject(Boundries))

{

inAir = false;

}

}

function scrollStage():void

{

Boundries.x += (stage.stageWidth * 0.5) - Player.x;

Player.x = stage.stageWidth * 0.5;

Boundries.y += (stage.stageHeight * 0.5) - Player.y;

Player.y = stage.stageHeight * 0.5;

}

function processCollisions():void

{

//  when player is falling

if (vy>0)

{

// process Collisions with boundries

var collision:Boolean = false;

if (Boundries.hitTestPoint(Player.x,Player.y,true))

{

collision = true;

}

if (collision)

{

while (collision)

{

Player.y -=  0.1;

collision = false;

if (Boundries.hitTestPoint(Player.x,Player.y,true))

{

collision = true;

}

}

vy = 0;

}

}

}

function JumpBtnHandler(e:TouchEvent):void

{

if (! inAir)

{

vy -=  20;

vx +=  5;

vx -=  5;

}

}

function RightBtnBegin(e:TouchEvent):void

{

addEventListener(Event.ENTER_FRAME, RunRight);

Player.scaleX = 1;

}

function RightBtnEnd(e:TouchEvent):void

{

removeEventListener(Event.ENTER_FRAME, RunRight);

}

function LeftBtnBegin(e:TouchEvent):void

{

addEventListener(Event.ENTER_FRAME, RunLeft);

Player.scaleX = -1;

}

function LeftBtnEnd(e:TouchEvent):void

{

removeEventListener(Event.ENTER_FRAME, RunLeft);

}

function RunRight(e:Event):void

{

Player.x +=  12;

}

function RunLeft(e:Event):void

{

Player.x -=  12;

}

function AttackBtnBegin(e:TouchEvent):void

{

Player.gotoAndStop(2);

}

function AttackBtnEnd(e:TouchEvent):void

{

Player.gotoAndStop(1);

}

stage.addEventListener(Event.ENTER_FRAME,HealthActions);

function HealthActions(e:Event):void

{

if (GlobalVaribles.Health == 5)

{

Health_Container.Health1.visible = true;

Health_Container.Health2.visible = true;

Health_Container.Health3.visible = true;

Health_Container.Health4.visible = true;

Health_Container.Health5.visible = true;

}

if (GlobalVaribles.Health == 4)

{

Health_Container.Health1.visible = true;

Health_Container.Health2.visible = true;

Health_Container.Health3.visible = true;

Health_Container.Health4.visible = true;

Health_Container.Health5.visible = false;

}

if (GlobalVaribles.Health == 3)

{

Health_Container.Health1.visible = true;

Health_Container.Health2.visible = true;

Health_Container.Health3.visible = true;

Health_Container.Health4.visible = false;

Health_Container.Health5.visible = false;

}

if (GlobalVaribles.Health == 2)

{

Health_Container.Health1.visible = true;

Health_Container.Health2.visible = true;

Health_Container.Health3.visible = false;

Health_Container.Health4.visible = false;

Health_Container.Health5.visible = false;

}

if (GlobalVaribles.Health == 1)

{

Health_Container.Health1.visible = true;

Health_Container.Health2.visible = false;

Health_Container.Health3.visible = false;

Health_Container.Health4.visible = false;

Health_Container.Health5.visible = false;

}

if (GlobalVaribles.Health == 0)

{

Health_Container.Health1.visible = false;

Health_Container.Health2.visible = false;

Health_Container.Health3.visible = false;

Health_Container.Health4.visible = false;

Health_Container.Health5.visible = false;

trace("Dead");

}

}

<AS>

And im getting a bug where when you go into a wall you automatically stand above it..

seen in this video

Also i would like to know a better way to side scroll well follow the character than

Boundries.x += (stage.stageWidth * 0.5) - Player.x;

Player.x = stage.stageWidth * 0.5;

Boundries.y += (stage.stageHeight * 0.5) - Player.y;

Player.y = stage.stageHeight * 0.5;

This topic has been closed for replies.

3 replies

Known Participant
September 15, 2011

if your gonna sell the game i dont recommend devnote's tutorial

you need a vCam

September 15, 2011

A vCam yes i know what one is but i thought the code for the vcam following the player doesnt work?

Colin Holgate
Inspiring
September 6, 2011

You can make some routines shorter. Like this for example:

function HealthActions(e:Event):void {

     Health_Container.Health1.visible = (GlobalVaribles.Health >= 1);

     Health_Container.Health2.visible = (GlobalVaribles.Health >= 2);

     Health_Container.Health3.visible = (GlobalVaribles.Health >= 3);

     Health_Container.Health4.visible = (GlobalVaribles.Health >= 4);

     Health_Container.Health5.visible = (GlobalVaribles.Health >= 5);

     if (GlobalVaribles.Health == 0) {

          trace("Dead");

     }

}

You have a While loop that is causing the man to move upwards until he is above the wall that he runs into. That's fine to stop him from falling, but it also gets triggered if he runs into a wall. If he collides after moving sidways, just move him back to where he was before the move.

September 7, 2011

How would i move him back? and thanks for the reorginisation of the health code..

=)

Colin Holgate
Inspiring
September 7, 2011

Like this:

function RunLeft(e:Event):void {

     var oldx:Number = Player.x;

     Player.x -=  12;

     if (Boundries.hitTestPoint(Player.x,Player.y,true)) {

          Player.x = oldx;

     }

}

September 6, 2011

Is this your whole class? It looks like timeline coding, if this is not your whole class can you paste all of it?