์ค๋์ ์ด์ ํ ๋ด์ฉ์ ์กฐ๊ธ ์ธ๋ฐํ๊ฒ ํด๋ณด๋ ์๊ฐ์ ๊ฐ๊ธฐ๋ก ํ์๋๋ฐ
์ด๋ฒ์๋ ๊ฒ์ ํด์๋๋ฅผ ์ข๋ ๋ชจ๋ฐ์ผ ์ค๋ฝ๊ฒ ๋ฐ๊พธ๊ณ
์บ๋ฆญํฐ ์กฐ์์ ์ข๋ ์ธ๋งํ๊ฒ ๋ง๋ค์ด๋ณด์
http://aaaga.dothome.co.kr/rpg1/index.html
์ค๋ ์์ ์ ๊ฒฐ๊ณผ๋ ์ฌ๊ธฐ์ ํ์ธ ํ ์ ์์
์ด๊ฑด ๋ด๊ฐ ๋ง๋ ๋ฆฌ์์ค์ธ๋ฐ ์ด๋ฒ์๋ ์ด๊ฑธ ์จ์ ํ ๊ฒ
์๋๋ ๊ทธ๋ฆผํ์ด๋ ๋ฉ๋ชจ์ฅ์ผ๋ก ๋ค ํ ๋ ค๊ณ ํ๋๋ฐ
๊ทธ๋ฆผํ์ผ๋ก ๊ทธ๋ฆผ ๊ทธ๋ฆฌ๊ธฐ ๊ท์ฐฎ๋ค. ๊ทธ๋ฅ ์ด๊ฑฐ ๋ค์ด๋ฐ์์ ์ฐ์ตํ์๊ตฌ
๊ธฐ์กด img.png ํ์ผ์ ์ง์ฐ๊ณ ์์ ๋๊ฐ์ ํ์ผ์ ๋ค์ด๋ฐ์์ img.png์ map.png๋ก ์ ์ฅํ์ด
์ด์ ์ฌ๊ธฐ์ ๋ง๊ฒ main.js ํ์ผ์ ์์ ํด ๋ณด์ ๋ณ๊ฒฝ๋ ๋ถ๋ถ๋ง ๋นจ๊ฐ๊ฒ ํ์ํด ๋ณผ๊ฒ
enchant(); window.onload = function(){
var winsize = window.innerWidth/window.innerHeight; //ํ๋ฉด์ ๊ฐ๋กํฌ๊ธฐ์ ์ธ๋กํฌ๊ธฐ์ ๋น๋ฅผ winsize์ ๋ฃ๋๋ค
var core = new Core(160*winsize.toFixed(2), 160); // ํ๋ฉด์ ๊ฐ๋ก์ฌ์ด์ฆ๋ 160 ๊ณฑํ๊ธฐ winsize์ ์์์ 2๋ฒ์งธ ์๋ฆฌ๊น์ง๋ฅผ ๊ณฑํ๊ฐ์ด๊ณ ์ธ๋ก๋ 160
core.preload ('img.png','map.png'); //map.png ํ์ผ์ ์ถ๊ฐ๋ก ๋ก๋ํจ
core.onload = function(){
core.rootScene.backgroundColor = 'yellow';
var label = new Label('์๋ ์ธ๊ณ!')
var sprite = new Sprite(16, 16);
sprite.image = core.assets['img.png'];
sprite.moveTo(16, 16);
sprite.frame = [0,0,0,0,1,1,1,1]; //๋ฐ๋ ์์ค์ ๋ง๊ฒ ํ๋ ์์ ์์ ํจ
//๊ธฐ์กด์ ์๋ up,down,left,right๋ ์ญ์ ํจ
var map = new Map(16, 16);
map.image = core.assets['map.png']; //์ถ๊ฐ๋ ์ด๋ฏธ์ง ํ์ผ๋ก ๋ณ๊ฒฝํจ
map.loadData([
[0,0,0,0,0,0,0,0,0,0],
[0,7,7,7,7,7,7,7,7,0],
[0,7,7,7,7,7,7,7,7,0],
[0,7,7,7,7,7,7,7,7,0],
[0,7,7,7,7,7,7,7,7,0],
[0,7,7,7,7,7,7,7,7,0],
[0,7,7,7,7,7,7,7,7,0],
[0,7,7,7,7,7,7,7,7,0],
[0,7,7,7,7,7,7,7,7,0],
[0,0,0,0,0,0,0,0,0,0]]); // ๋ฐ๋ ํ์ผ์ ๋ง๊ฒ ๊ทธ๋ฆผ๋ฒํธ๋ฅผ ๋ณ๊ฒฝํด์ค
map.collisionData = [
[0,0,0,0,0,0,0,0,0,0],
[0,1,1,1,1,1,1,1,1,0],
[0,1,1,1,1,1,1,1,1,0],
[0,1,1,1,1,1,1,1,1,0],
[0,1,1,1,1,1,1,1,1,0],
[0,1,1,1,1,1,1,1,1,0],
[0,1,1,1,1,1,1,1,1,0],
[0,1,1,1,1,1,1,1,1,0],
[0,1,1,1,1,1,1,1,1,0],
[0,0,0,0,0,0,0,0,0,0]]; // ๊ฐํ์ผ์ด ์ง๋ ์ ์๋ ํ์ผ์ด๋ฉด 0, ์ง๋ ์ ์์ผ๋ฉด 1์ ์ง์ด๋ฃ์์
var controller0 = new Sprite(32, 32); //controller0 ์ด๋ผ๋ ์ค๋ธ์ ํธ๋ฅผ ๋ง๋ฌ ๊ทธ๋ฆผ์ด๊ณ ํฌ๊ธฐ๋ 32x32ํฝ์
controller0.backgroundColor = 'black'; //๊ทธ๋ฆผ์ ์๊น์ ๊ฒ์์์ผ๋ก ํ๋ค
controller0.opacity = 0.2; //ํฌ๋ช ๋๋ฅผ 20%๋ก ํ๋ค
var controller1 = new Sprite(16, 16); //controller1 ์ด๋ผ๋ ์ค๋ธ์ ํธ๋ฅผ ๋ง๋ ๋ค ๊ทธ๋ฆผ์ด๊ณ ํฌ๊ธฐ๋ 16x16ํฝ์
controller1.backgroundColor = 'black'; //์๊น์ ๊ฒ์์์ผ๋ก
controller1.opacity = 0.3; //ํฌ๋ช
๋๋ฅผ 30%๋ก
var movego ={}; //movego ๋ผ๋ ์ค๋ธ์ ํธ๋ฅผ ๋ง๋ ๋ค. ๊ฐ์ฒด(์์ฑ์ ๊ฐ์ง ๋ฐ์ดํฐ)์.
var movend ={}; //movend ๋ผ๋ ์ค๋ธ์ ํธ๋ฅผ ๋ง๋ ๋ค.
var angle = 0; //angle ์ด๋ผ๋ ์ค๋ธ์ ํธ๋ฅผ ๋ง๋ ๋ค. ์ซ์๋ฅผ ์ ์ฅํ๋ค.
var toward = 0; //toward ๋ผ๋ ์ค๋ธ์ ํธ๋ฅผ ๋ง๋ ๋ค. ์ซ์๋ฅผ ์ ์ฅํ๋ค.
core.rootScene.on('touchstart', function(e){ //๊ฒ์ ํ๋ฉด์ ํฐ์นํ ๋ ์คํํ ๋ด์ฉ (์ {๊ฐ ์ด๋ ค์์์ ์ฃผ์
movego.x = e.x; // movego ๊ฐ์ฒด x์์ฑ์ e.x (ํฐ์นํ ๊ณณ์ x์ขํ)๋ผ๋ ๊ฐ์ ๋ฃ๋๋ค.
movego.y = e.y; // movego ๊ฐ์ฒด y์์ฑ์ e.yย (ํฐ์นํ ๊ณณ์ y์ขํ)๋ผ๋ ๊ฐ์ ๋ฃ๋๋ค.
controller0.moveTo(movego.x -16, movego.y-16); //controller0๋ฅผ ํด๋น ์ขํ๋ก ์ด๋์ํด
core.rootScene.addChild(controller0); //controller0๋ฅผ ํ๋ฉด์ ๋ํ๋ธ๋ค
core.rootScene.addChild(controller1); //controller1์ ํ๋ฉด์ ๋ํ๋ธ๋ค
}); //core.rootScene.on('touchstart', function(e){ ์๊ดํธ๋ฅผ ๋ซ๋๋ถ๋ถ
core.rootScene.on('touchmove', function(e){ //๊ฒ์ ํ๋ฉด์ ์์ ๋ผ์ง ์์ ์ํ์์ ์์ง์ผ ๊ฒฝ์ฐ ์คํํ ๋ด์ฉ (์ {๊ฐ ์ด๋ ค์์์ ์ฃผ์
movend.x = e.x; // movend ๊ฐ์ฒด x์์ฑ์ e.x (์์ง์ธ ๊ณณ์ x์ขํ)๋ผ๋ ๊ฐ์ ๋ฃ๋๋ค.
movend.y = e.y; // movend ๊ฐ์ฒด y์์ฑ์ e.x (์์ง์ธ ๊ณณ์ y์ขํ)๋ผ๋ ๊ฐ์ ๋ฃ๋๋ค.
angle = Math.atan2(movego.x-movend.x,movego.y-movend.y); //์ฒ์ ํฐ์นํ ์ ์์ ๋์ค์ ์์ง์ธ ์ ๊น์ง์ ๊ฐ๋๋ฅผ ์ธก์ ํด์ angle์ ์ ์ฅ
if (-0.78539<angle && angle<0.78539) {toward = 1}; //angle์ด -ฯ/4 ๋ณด๋ค ํฌ๊ณ ฯ/4๋ณด๋ค ์์๊ฒฝ์ฐ์๋ toward์ 1์ ์ ์ฅํ๋ค.
if (-2.35619<angle && angle<-0.78539) {toward = 2}; //angle์ด -3ฯ/4 ๋ณด๋ค ํฌ๊ณ -ฯ/4๋ณด๋ค ์์๊ฒฝ์ฐ์๋ toward์ 2๋ฅผ ์ ์ฅํ๋ค.
if (0.78539<angle && angle <2.35619) {toward = 3}; //angle์ด ฯ/4 ๋ณด๋ค ํฌ๊ณ 3ฯ/4๋ณด๋ค ์์๊ฒฝ์ฐ์๋ toward์ 3์ ์ ์ฅํ๋ค.
if (2.35619<angle && angle<3.14159 || -3.14159<angle && angle<-2.35619) {toward = 4} //angle์ด 3ฯ/4 ๋ณด๋ค ํฌ๊ณ ฯ๋ณด๋ค ์์๊ฒฝ์ฐ ๋๋ -ฯ๋ณด๋ค ํฌ๊ณ -3ฯ/4๋ณด๋ค ์์ ๊ฒฝ์ฐ์๋ toward์ 4๋ฅผ ์ ์ฅํ๋ค.
controller1.moveTo(movend.x-8, movend.y-8); //controller1์ ํด๋น ์ขํ๋ก ์ด๋์ํด
}); //core.rootScene.on('touchmove', function(e){ ์๊ดํธ๋ฅผ ๋ซ๋๋ถ๋ถ
core.rootScene.on('touchend', function(e){ //๊ฒ์ ํ๋ฉด์ ์์ ๋ผ์์๊ฒฝ์ฐ ์คํํ ๋ด์ฉ (์ {๊ฐ ์ด๋ ค์์์ ์ฃผ์
toward = 0; // toward์ 0์ ์ ์ฅํ๋ค.
core.rootScene.removeChild(controller0); //controller0์ ํ๋ฉด์์ ์ ๊ฑฐํ๋ค
core.rootScene.removeChild(controller1); //controller0์ ํ๋ฉด์์ ์ ๊ฑฐํ๋ค
}); //core.rootScene.on('touchend', function(e){ ์๊ดํธ๋ฅผ ๋ซ๋๋ถ๋ถ
core.rootScene.on('enterframe', function(){ //๊ฒ์์ด ์คํ๋๋ ๋์ ํญ์ ์คํํ ๋ด์ฉ (์ {๊ฐ ์ด๋ ค์์์ ์ฃผ์
label.text = toward; // label์ ๋ด์ฉ์ toward์ ์ ์ฅ๋ ๊ฐ์ผ๋ก ๋ฐ๊พผ๋ค
switch(toward){ //toward ์ ์ฅ๋ ์ซ์์ ๋ฐ๋ผ ์๋์ ๋ด์ฉ์ ์คํํ๋ผ๋ ์๋ฏธ {๊ฐ ์ด๋ ค์์์ ์ฃผ์
case 0: break; // toward๊ฐ 0์ผ๊ฒฝ์ฐ ์๋ฌด๊ฒ๋ ํ์ง ์๋๋ค.
case 1: sprite.frame = [6,6,6,6,7,7,7,7]; //toward๊ฐ 1์ผ๊ฒฝ์ฐ sprite์ ๊ทธ๋ฆผ๋ฒํธ๋ฅผ ํด๋น๋ฒํธ๋ก ๋ฐ๊พผ๋ค.
if (map.hitTest(sprite.x, sprite.y-1)){sprite.moveBy(0,-1);}; break; //ํด๋น ์ขํ์ map.hitTest๊ฐ์ด true๋ผ๋ฉด sprite๋ฅผ ํด๋น ์ขํ๋ก ์ด๋
case 2: sprite.frame = [2,2,2,2,3,3,3,3]; //toward๊ฐ 2์ผ๊ฒฝ์ฐ sprite์ ๊ทธ๋ฆผ๋ฒํธ๋ฅผ ํด๋น๋ฒํธ๋ก ๋ฐ๊พผ๋ค
if (map.hitTest(sprite.x+16, sprite.y)){sprite.moveBy(1,0);}; break; //ํด๋น ์ขํ์ map.hitTest๊ฐ์ด true๋ผ๋ฉด sprite๋ฅผ ํด๋น ์ขํ๋ก ์ด๋
case 3: sprite.frame = [4,4,4,4,5,5,5,5]; //toward๊ฐ 3์ผ๊ฒฝ์ฐ sprite์ ๊ทธ๋ฆผ๋ฒํธ๋ฅผ ํด๋น๋ฒํธ๋ก ๋ฐ๊พผ๋ค
if (map.hitTest(sprite.x-1, sprite.y)){sprite.moveBy(-1,0);}; break;
case 4: sprite.frame = [0,0,0,0,1,1,1,1]; //toward๊ฐ 4์ผ๊ฒฝ์ฐ sprite์ ๊ทธ๋ฆผ๋ฒํธ๋ฅผ ํด๋น๋ฒํธ๋ก ๋ฐ๊พผ๋ค;
if (map.hitTest(sprite.x, sprite.y+16)){sprite.moveBy(0,1);}; break; //ํด๋น ์ขํ์ map.hitTest๊ฐ์ด true๋ผ๋ฉด sprite๋ฅผ ํด๋น ์ขํ๋ก ์ด๋
}; // switch(toward){ ์ ๊ดํธ๋ฅผ ๋ซ๋ ๋ถ๋ถ
}); //core.rootScene.on('enterframe', function(e){ ์๊ดํธ๋ฅผ ๋ซ๋๋ถ๋ถ
core.rootScene.addChild(map);
core.rootScene.addChild(sprite);
core.rootScene.addChild(label); //๋๊ฐ์ ์์๊ฐ ๋ฐ๋์์. ๋ฐ์ ์๋๊ฒ ์ ์ผ ์์ ๋ํ๋
};
core.start();
};
์ด๊ธ์ ๋ณด๋ฉฐ ๋ฐ๋ผํ๋ ์ฌ๋๋ค์
๊ทธ๋ฅ ๋ณต์ฌ ๋ถ์ฌ๋ฃ๊ธฐ๋ง ํ์ง ๋ง๊ณ ํด๋น ๋ด์ฉ์ ์๊ธฐ ์ ๋ง๋๋ก ๋ฐ๊พธ๋ฉด์ ์ด๋ป๊ฒ ๋ฐ๋๋์ง
์๋ก๊ณ ์นจ์ ๋ฐ๋ณตํ๋ฉด์ ํ์ธํด๋ณด๋ฉด ์ค๋ ฅ์ด ๊ธ๋ฐฉ ๋๊ฑฐ์ผ
๋ณต์กํด ๋ณด์ฌ๋ ๋ฏ์ด๋ณด๋ฉด ์ง์ง ๋ณ๊ฑฐ ์๋ค. ํ์ดํ !
๋๊ธ 0