Mapを使おう | Webプログラミング!

Time-stamp: "2013-12-11 Wed 18:55 JST hig"

Map

Map

map1.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <title>Test of Enchant.js</title>
    <style type="text/css">
      body { margin: 0; }
    </style>

    <script type="text/javascript" src="enchant.js"></script>
    <script type="text/javascript" src="plugins/ui.enchant.js"></script>
    <script type="text/javascript" src="map1.js"></script>
  </head>
  <body>
  </body>
</html>

map1.js

enchant();

window.onload = function() {
    var game = new Game(320,320);
    game.fps = 16;
    game.preload('chara1.png','map0.png');

    game.onload = function() {

	var m=new Map(16,16); // tile of a single tile
	m.image = game.assets['map0.png'];
	m.loadData(
	    // 1層目
	    [
		[0,0,0,0],
		[0,0,0,0],
		[0,2,2,0],
		[0,0,0,-1]
	    ],
	    // 2層目
	    [
		[-1,-1,19,-1],
		[-1,16,19,-1],
		[-1,16,-1,-1],
		[-1,-1,-1,-1]
	    ]
	);
	// 衝突判定データ
	m.collisionData = [
		[0,0,0,0],
		[0,1,0,0],
		[0,1,0,0],
		[0,0,0,0]
	];
	game.rootScene.addChild(m);

	var bear = new Sprite(32,32);
	bear.image = game.assets['chara1.png'];
	game.rootScene.addChild(bear);
	bear.frame = 7;
	bear.scaleX=0.25;
	bear.scaleY=0.25;
	var d=4;
//	var s=2;

	bear.moveTo(160,0);
	bear.addEventListener(Event.ENTER_FRAME, 
			      function() {
				  var input=game.input;
				  if( input.left ){
				      this.moveBy(-d,0);
				  } else if ( input.right ){
 				      this.moveBy(+d,0);
				  } else if ( input.up ){
				      this.moveBy(0,-d);
				  } else if ( input.down ){
				      this.moveBy(0,+d);
				  } else if ( input.a ){
				      this.scaleX*=2;
				  } else if ( input.b ){
				      this.scaleX*=-1;
				  }
				  if(m.hitTest(this.x+16,this.y+16)){
				      // タイル1個分ずれる
				      this.frame=2;
				  }else{
				      this.frame=7;
				  }
			      }
			     );

    };

    game.start();
};

このサイトのコンテンツ

QRcode to hig3.net

http://hig3.net