platform_game

hitTestObject with platform

Page 1 of 1

2 Replies - 674 Views - Last Post: 03 November 2009 - 11:09 PM

#1 phingko  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 01-November 09

platform_game

Posted 03 November 2009 - 07:48 AM

Yo!


I'm trying to do a platform game now.

I want my cup to be controlled by user,jumping around the platform to catch the ingredients.

However, only at my fourth platform, though my cup can stand there, it can't jump at all.

And the other problem is, if I left the cup like for 3 seconds,it will falling down off the stage!

This is weird.


I think the problem probably in my code; but I am so sick to look at it again and again.

Can anyone point me where my mistake?



Here's the code;


// -------------------------- THE CUP

var cup:Cup= new Cup();
addChild(cup);
cup.x = 250;
cup.y= 50;
cup.scaleX = cup.scaleY = 3/4


// -------------------------- THE PLATFORM


var block_array:Array = new Array();
const BLOCK_LENGTH:Number = 3;
var block:Sprite;
var block2:Sprite;
function drawBlock():void {
	for (var i:Number=0;i<BLOCK_LENGTH; i++) {
		block = new Sprite();
		block.graphics.lineStyle(1,0x603812);
		block.graphics.beginFill(0x603812);
		block.graphics.drawRect(0,0,60,10);
		block.x = i*230+32;
		block.y = i*(-100)+350;
		addChild (block);
		block_array.push(block);
	}
	for (var j:Number=0;j<BLOCK_LENGTH; j++) {
		block2 = new Sprite();
		block2.graphics.lineStyle(1,0x603812);
		block2.graphics.beginFill(0x603812);
		block2.graphics.drawRect(0,0,40,10);
		block2.x = j*120+120;
		block2.y = j*(-10)+300;
		addChild (block2);
		block_array.push(block2);
	}
}

drawBlock();

//to make some of the array's index in different position
block_array[2].x = 410;
block_array[2].y = 400;
block_array[4].x = 190;
block_array[4].y = 300;

var fallSpeed:Number = 0; 
const GRAVITY:Number = 1; 
const GROUND_Y:Number = stage.stageHeight;
var isJumping:Boolean = false;
var hSpeed:Number = 0;

function jump(cup_mc:MovieClip):void { 
	cup_mc.y += fallSpeed;
	cup_mc.x += hSpeed;
	if (cup_mc.y > GROUND_Y) {
		trace("die");
		isJumping = false;
	}
	hitPlatform(cup);
}

function hitPlatform(cup_mc:MovieClip):void {
	for(var i:Number =0;i<block_array.length; i++) {
		if (cup_mc.hitTestObject(block_array[i])) {
			cup_mc.y = block_array[i].y-40;
			isJumping = true;
			trace(cup_mc.y );
		} else {
			isJumping = false;
		}
	}catchIng(cup)
}




Please.

Thanks in advanced

Is This A Good Question/Topic? 0
  • +

Replies To: platform_game

#2 thehat  Icon User is offline

  • awake ? web();
  • member icon

Reputation: 106
  • View blog
  • Posts: 951
  • Joined: 28-February 08

Re: platform_game

Posted 03 November 2009 - 09:40 AM

I think you're missing some vital information here. The catchIng function is undefined, and the cup doesn't seem to do anything. Should there be some code for the cup class?
Was This Post Helpful? 0
  • +
  • -

#3 phingko  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 01-November 09

Re: platform_game

Posted 03 November 2009 - 11:09 PM

View Postthehat, on 3 Nov, 2009 - 08:40 AM, said:

I think you're missing some vital information here. The catchIng function is undefined, and the cup doesn't seem to do anything. Should there be some code for the cup class?


this is the catIng function. I just thought the problem was not from this code, so I didn't put it in my first post.
My cup has a keyboard event,where user can control left and right and space bar to jump.

function catchIng(cup_mc:MovieClip):void {
	for (var i:Number = 0; i<ingredients_array.length; i++ ) {
		if (cup_mc.hitTestObject(ingredients_array[i])) {
			temp_array.push(ingredients_array[i]);
			removeChild(ingredients_array[i]);
			ingredients_array.splice(i,1);
			trace(temp_array);
		}
		
	}
	if (temp_array == cap_array) {
			trace("cappuccino");
		}
}

var fallSpeed:Number = 0; 
const GRAVITY:Number = 1; 
const GROUND_Y:Number = 660;
var isJumping:Boolean = false;
var hSpeed:Number = 0;

function onKeyRelease(evt:KeyboardEvent):void { 
	if ((evt.keyCode == Keyboard.SPACE) && !isJumping ){
	fallSpeed = -13;
	isJumping = true;
	}
}

function detectKey(cup_mc:MovieClip):void { 
	if (Key.isDown(Keyboard.LEFT)) {
	cup_mc.x -= 5; 
	} else if (Key.isDown(Keyboard.RIGHT)) {
	cup_mc.x += 5;
	}
}



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1