FLASH: hitTest, creating more than one wall.

FLASH: making more than one wall.

Page 1 of 1

2 Replies - 3217 Views - Last Post: 12 July 2008 - 08:52 AM Rate Topic: -----

#1 cadeownz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 12-July 08

FLASH: hitTest, creating more than one wall.

Post icon  Posted 12 July 2008 - 01:00 AM

I have made a simple RPG Game (Just Started) and i need to make more than ONE solid wall. Any help?
Code Below...


onClipEvent (enterFrame) {
	if (_root.go == true) {
		yspeed = yspeed+gravity;
		for (x=1; x<precision; x++) {
			spot_x = _x+radius*Math.sin(x*360/precision);
			spot_y = _y-radius*Math.cos(x*360/precision);
			if (_root.terrain.hitTest(spot_x, spot_y, true)) {
				yspeed = 0;
				}
		}
		_y = _y+yspeed;
		_x = _x+xspeed;
	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: FLASH: hitTest, creating more than one wall.

#2 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1147
  • View blog
  • Posts: 7,134
  • Joined: 07-September 06

Re: FLASH: hitTest, creating more than one wall.

Posted 12 July 2008 - 02:22 AM

There are many ways to make this work out, you may want to look at google for "console games" there is one that shows how to make an interestin console type game in flash.

One way that you may want to think about doing things, assuming that you don't have millions of different pieces of terrain on the screen at once (or checking at the same time) would be to add all of them to an array and just loop through them.

var terrain_array:Array = new Array();
	terrain_array.push(_root.terrain);
	//and so on...
onClipEvent(enterFrame){
	if(_root.go == true){
		yspeed = yspeed+gravity;
		for(var x:Number = 1; x<precision; x++){
			spot_x = _x+radius*Math.sin(x*360/precision);
			spot_y = _y-radius*Math.cos(x*360/precision);
			doHitCheck(spot_x, spot_y);
		}
	}
}
function doHitCheck(x:Number, y:Number){
	for(var i:Number = 0; i<terrain_array.length; i++){
		if(terrain_array[i].hitTest(x, y, true)){
			yspeed = 0;
			break;
		}
	}
	return;
}


That will check for everything you add into the terrain and see if every check hits it, if it does it will end the loop and continue the checks, other wise it will continue the loop until it finds one that works, or finishes going through the array of movieclips.

However, a better way, is to simply place all the ground that you want in a single movieclip and then to a hitTest against it.

Here is some sample code from a console game out there in one of those tutorials:

var jumpHeight = -20;

var options = _root.createEmptyMovieClip("opts",101);

var k = _root.createEmptyMovieClip("square", l00);
k.beginFill(0x989cc9);
k.moveTo(0, -5);
kpt = new Array({x:0, y:-5}, {x:0, y:5}, {x:5, y:5}, {x:5, y:-5});
for (var e = 1; e<kpt.length; e++) {
	k.lineTo(kpt[e].x, kpt[e].y);
}
sp = 0;
k._y = Stage.height/2;
k._x = Stage.width/2;
pts = new Array({x:23, y:-200}, {x:46, y:-200}, {x:46, y:333}, {x:233, y:333}, {x:290.9, y:273}, {x:480, y:332.4}, {x:620, y:266}, {x:620, y:230}, {x:641, y:230}, {x:641, y:176}, {x:681, y:176}, {x:681, y:250}, {x:740, y:335}, {x:900, y:335}, {x:900, y:-200}, {x:940, y:-200}, {x:940, y:381}, {x:23, y:382}, {x:23, y:-200});
_root.createEmptyMovieClip("plat", -1);
plat.lineStyle(10);
for (var i = 1; i<pts.length; i++) {
	plat.moveTo(pts[i-1].x, pts[i-1].y);
	plat.lineTo(pts[i].x, pts[i].y);
}
onEnterFrame = function () {
	if(plat.hitTest(square._x, square._y, true)){
		jumping=false;
	}
	else{
		square._y += 8;
	}
	while (plat.hitTest(square._x, square._y, true)) {
		square._y--;
	}
	if((!plat.hitTest(square._x+10, square._y-20, true) && sp<-1) || (!plat.hitTest(square._x-10, square._y-20, true) && sp>1)){
		plat._x += (sp *= .85);
	}
	else{
		(sp *= .85);
	}
	if(Key.isDown(Key.UP) && !jumping){
		jump=jumpHeight;
		jumping=true;
	}
	else{
		null;
	}

	if(jumping){
		if(jump<10){
			jump++;
		}
		else{
			jump=10;
		}
		square._y += jump;
	}
	else{
		null;
	}
	if(Key.isDown(Key.LEFT)){
		sp += .75;
	}
	else{
		null;
	}
	if(Key.isDown(Key.RIGHT)){
		sp--;
	}
	else{
		null;
	}

};


They just make a single movieclip and do all their checks agains it for grouund and walls.

Hope that helps.
Was This Post Helpful? 0
  • +
  • -

#3 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 999
  • View blog
  • Posts: 4,175
  • Joined: 14-February 08

Re: FLASH: hitTest, creating more than one wall.

Posted 12 July 2008 - 08:52 AM

question answered :)

god i love flash , makes collision detection so easy!

This post has been edited by stayscrisp: 12 July 2008 - 01:09 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1