충돌 감지에 문제가 있습니다. 기본적으로 단단한 타일을 칠 때 캐릭터의 몸은 이미 타일의 절반 정도입니다. 여기 내 코드가있다.타일 기반 환경에서 충돌 감지
속성 wY와 wX는 내 게임 세계 위치입니다. 무대 위치가 아닙니다. dx와 dy는 캐릭터가 이동하는 속도입니다. 이 첫 번째 코드 스 니펫은 게임 루프 내에 있습니다. 내 캐릭터의 초점 포인트는 x 축 여기
package com.objects
{
import flash.display.MovieClip;
import com.eapi.EngineApi;
import flash.events.Event;
/**
* ...
* @author Anthony Gordon
*/
public class Engine extends EngineApi
{
public var friction:Number = 0.93;
protected var Heros:Array;
public function Engine(w:Number = 540,h:Number = 360, tw:Number = 50, th:Number = 50)
{
super(w, h, tw, th);
Heros = new Array();
}
override protected function loop(e:Event):void
{
UpdateObjects();
Rules();
CheckHero();
UpDateMap();
}
public function AddHero(g:GameObject):void
{
Heros.push(g);
}
protected function Rules():void
{
//Everything Has friction
for (var i:Number = 0; i < gameObjects.length; i++)
{
var char:GameObject = GameObject(gameObjects[i]);
char.dx *= friction;
//char.dy *= friction;
//Below is the tile positioning of my character
var cgridx:Number = Math.floor(char.wX/tileW);
var cgridy:Number = Math.floor(char.wY/ tileH);
//This is the tile in front of the character
var nextx:Number = Math.floor((char.wX + char.dx)/tileW);
var nexty:Number = Math.floor((char.wY + char.dy)/tileH);
//We assume the character is in the air before we figure it to be false
char.onGround = false;
//I am about to remove the vars from cgrid below. Keep a look out for issues in the future
if (mapHolder[currentMap][nexty][cgridx] == 0 || mapHolder[currentMap][nexty][cgridx] == 2)
{
//If character is falling down
if (char.dy > 0)
{
char.wY = (nexty * tileH) - 1;
cgridy = Math.floor(char.wY/tileH);
char.dy = 0;
char.onGround = true;
}
else if (char.dy < 0)//If character is going up
{
char.wY = (nexty * tileH) + (tileH + 1);
cgridy = Math.floor(char.wY/tileH);
char.dy = 0;
}
}
//mapHolder is a array that holds an array of maps and their tile numbers
if (mapHolder[currentMap][cgridy][nextx] == 2)
{
if (char.dx > 0)//if character is going right
{
char.wX = ((nextx * tileW) - 1);
}
else if (char.dx < 0)// if character is going left
{
char.wX = (nextx * tileW) + (tileW + 1);
}
char.dx = 0;
}
//if character is not on ground then keep faling
if (char.onGround == false)
{
char.dy += .9;
if (char.dy > 30) char.dy = 5;
}
}
}
protected function CheckHero():void
{
var char:Hero = Heros[0];
char.x = char.wX - offsX;
char.y = char.wY - offsY;
if (char.wX < 0)
{
char.wX = 0;
char.dx = 0;
}
if (char.wY < 0)
{
char.wY = 0;
char.dy = 0;
}
offsX = char.wX - (vWidth/2);
offsY = char.wY - (vHeight/2);
if (offsX < 0)
{
offsX = 0;
}
if (offsY < 0)
{
offsY = 0;
}
//If screen hits the world END STOP!!!
if ((offsX + vWidth) > wWidth)
{
offsX = (wWidth - vWidth);
}
if ((offsY + vHeight) > wHeight)
{
offsY = (wHeight - vHeight);
}
/////
//If char hits the end, Stop!!
if (char.wX > wWidth)
{
char.wX = char.wX - wWidth;
char.wX = wWidth;
}
}
}
}
을 중심으로하는 내 문자 클래스
package com.objects
{
import flash.display.MovieClip;
import flash.events.*;
/**
* ...
* @author Anthony Gordon
*/
[Embed(source='../../../bin/Assets.swf', symbol='Hero')]
public class Hero extends GameObject
{
private var aKeyPress:Array;
private var jumpDisabled:Boolean = false;
public function Hero()
{
wY = 150;
wX = 90;
speed = .5;
aKeyPress = new Array();
TheGame.sr.addEventListener(KeyboardEvent.KEY_DOWN, keyDownListener);
TheGame.sr..addEventListener(KeyboardEvent.KEY_UP,keyUpListener);
}
private function keyDownListener(e:KeyboardEvent):void {
//trace("down e.keyCode=" + e.keyCode);
aKeyPress[e.keyCode]=true;
}
private function keyUpListener(e:KeyboardEvent):void {
//trace("up e.keyCode=" + e.keyCode);
aKeyPress[e.keyCode]=false;
}
override public function UpdateObject():void
{
Controls();
updatePosition();
}
private function Controls():void
{
wX += dx;
wY += dy;
if (aKeyPress[38])//Key press up
;//vy -= speed;
else if (aKeyPress[40])//Key press down
;//dy += speed;
if (aKeyPress[37])//left
dx -= speed;
else if (aKeyPress[39])//Right
dx += speed;
if (aKeyPress[32]){//space
jump();
}
}//End Controls
private function jump():void
{
if (!jumpDisabled)
{
if (onGround)
{
dy = -15;
jumpDisabled = true;
}
}
else
{
jumpDisabled = false;
}
}
}
}
char.wX = (nextx * tileW) + (tileW + 1); 나는 책에서 그것을 참조했다. 나는 왜 그가 그것을 사용하는지 모르겠습니다. 아마 나의 초점 포인트는 영웅의 왼쪽에 있어야한다. 그리고, 영웅은 규칙적인 픽셀 단위로 움직일 수 있습니다. 코드를 업데이트했습니다. Wx와 Wy는 게임 세계에서의 포지셔닝입니다. 그들은 여전히 픽셀과 같습니다. – numerical25