StartBlock class Start extends Actor main new Player












ブロックを増やそう! • 「Start」にプログラムを追加して「Block」を増やしましょう class Start extends Actor { *main() { new Player({ x: 30 , y: 100 , radius: 10}); new Block({x: 20, y: 300, scale. X: 5, scale. Y: 1, p: 2}); new Block({x: 300, y: 200, scale. X: 5, scale. Y: 1, p: 2}); } } • ブラウザを再読み込みすると「Block」が一枚増える 12

プレイヤーをジャンプさせよう! • 「Player」にプログラムを追加して「Player」がジャンプできるようにしま しょう class Player extends Body. Actor { *main() { while(true){ if(this. getkey("right")>0) { this. vx+=0. 6; } if(this. getkey("left")>0) { this. vx-=0. 6; } if(this. getkey("space")==1 && this. contact. To(Block)){ this. vy=-10; } yield; } } } • ブラウザを再読み込みするとスペースキーで「Player」がジャンプ 13


画面をスクロールさせよう! • 「Player」にプログラムを追加して画面が「Player」についてく るようにしましょう while(true){ if(this. getkey("right")>0 || window. $right. B. clicked) { this. vx+=0. 6; } if(this. getkey("left")>0 || window. $left. B. clicked) { this. vx-=0. 6; } if( (this. getkey("space")==1 ||window. $jump. B. clicked) && this. contact. To(Block)){ this. vy=-10; } $Screen. scroll. To(this. x-$screen. Width/2, this. y-$screen. Height/2); yield; } • ブラウザを再読み込みすると「Player」が画面の中心になる 15

スマホで動かせるようにしよう!(1) • 今のままではスマホで動かすことができない… • ボタンをゲームに登場させる • Startに追加 class Start extends Actor { *main() { window. $left. B=new Button({left: 10, top: 400, width: 80, text: "Left"}); window. $right. B=new Button({left: 100, top: 400, width: 80, text: "Right"}); window. $jump. B=new Button({left: 300, top: 400, width: 80, text: "Jump"}); (これより下は省略) • 実行すると画面にボタンが出てくる 16

スマホで動かせるようにしよう!(2) • このままではプレイヤーがボタンに反応しない • Playerのコードを一部追加 while(true){ if(this. getkey("right")>0 || window. $right. B. clicked) { this. vx+=0. 6; } if(this. getkey("left")>0 || window. $left. B. clicked) { this. vx-=0. 6; } if( (this. getkey("space")==1 ||window. $jump. B. clicked) && this. contact. To(Block)){ this. vy=-10; } $Screen. scroll. To(this. x-$screen. Width/2, this. y-$screen. Height/2); yield; } • 実行するとボタンでも操作できるようになる 17

- Slides: 18