fix(canvas): emit should call with view as this

fix(sample): 贪吃蛇重力感应版: some typo and bugs
This commit is contained in:
hyb1996
2018-03-31 12:25:38 +08:00
parent 7a4eb33245
commit 32f9fd3359
5 changed files with 40 additions and 41 deletions

View File

@@ -8,8 +8,8 @@ android {
applicationId "com.stardust.scriptdroid" applicationId "com.stardust.scriptdroid"
minSdkVersion 17 minSdkVersion 17
targetSdkVersion 23 targetSdkVersion 23
versionCode 258 versionCode 259
versionName "3.1.1 Alpha2" versionName "3.1.1 Alpha3"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
multiDexEnabled true multiDexEnabled true
ndk { ndk {

View File

@@ -28,11 +28,11 @@ const MOVE_INTERVAL = 500;
//方块宽度 //方块宽度
const BLOCK_WIDTH = 40; const BLOCK_WIDTH = 40;
//游戏区域宽高 //游戏区域宽高
const GAME_BORAD_HEIGHT = 20; const GAME_BOARD_HEIGHT = 20;
const GAME_BORAD_WIDTH = 15; const GAME_BOARD_WIDTH = 15;
//蛇的四个移动方向 //蛇的四个移动方向
const DIRECTION_LFET = {x: -1, y: 0}; const DIRECTION_LEFT = {x: -1, y: 0};
const DIRECTION_RIGHT = {x: 1, y: 0}; const DIRECTION_RIGHT = {x: 1, y: 0};
const DIRECTION_UP = {x: 0, y: -1}; const DIRECTION_UP = {x: 0, y: -1};
const DIRECTION_DOWN = {x: 0, y: 1}; const DIRECTION_DOWN = {x: 0, y: 1};
@@ -62,24 +62,24 @@ ui.board.on("draw", function(canvas){
} }
//计算坐标偏移,是的游戏区域绘制在画面的水平居中位置 //计算坐标偏移,是的游戏区域绘制在画面的水平居中位置
var offset = { var offset = {
x: (canvas.getWidth() - (GAME_BORAD_WIDTH + 2) * BLOCK_WIDTH) / 2, x: (canvas.getWidth() - (GAME_BOARD_WIDTH + 2) * BLOCK_WIDTH) / 2,
y: 100 y: 100
}; };
//偏移坐标 //偏移坐标
canvas.translate(offset.x, offset.y); canvas.translate(offset.x, offset.y);
//绘制围墙 //绘制围墙
paint.setColor(WALL_COLOR); paint.setColor(WALL_COLOR);
for(var i = 0; i <= GAME_BORAD_WIDTH + 1; i++){ for(var i = 0; i <= GAME_BOARD_WIDTH + 1; i++){
//上围墙 //上围墙
drawBlock(canvas, paint, i, 0); drawBlock(canvas, paint, i, 0);
//下围墙 //下围墙
drawBlock(canvas, paint, i, GAME_BORAD_HEIGHT + 1); drawBlock(canvas, paint, i, GAME_BOARD_HEIGHT + 1);
} }
for(var i = 0; i <= GAME_BORAD_HEIGHT + 1; i++){ for(var i = 0; i <= GAME_BOARD_HEIGHT + 1; i++){
//左围墙 //左围墙
drawBlock(canvas, paint, 0, i); drawBlock(canvas, paint, 0, i);
//右围墙 //右围墙
drawBlock(canvas, paint, GAME_BORAD_WIDTH + 1, i); drawBlock(canvas, paint, GAME_BOARD_WIDTH + 1, i);
} }
//绘制蛇身 //绘制蛇身
paint.setColor(SNAKE_COLOR); paint.setColor(SNAKE_COLOR);
@@ -95,7 +95,7 @@ ui.board.on("draw", function(canvas){
var gameThread = threads.start(game); var gameThread = threads.start(game);
//按键点击时改变蛇的移动方向 //按键点击时改变蛇的移动方向
ui.left.on("click", ()=> direction = DIRECTION_LFET); ui.left.on("click", ()=> direction = DIRECTION_LEFT);
ui.right.on("click", ()=> direction = DIRECTION_RIGHT); ui.right.on("click", ()=> direction = DIRECTION_RIGHT);
ui.up.on("click", ()=> direction = DIRECTION_UP); ui.up.on("click", ()=> direction = DIRECTION_UP);
ui.down.on("click", ()=> direction = DIRECTION_DOWN); ui.down.on("click", ()=> direction = DIRECTION_DOWN);
@@ -139,10 +139,9 @@ function generateApple(){
//循环生成苹果直至苹果不会生成在蛇身上 //循环生成苹果直至苹果不会生成在蛇身上
var x, y; var x, y;
do{ do{
x = random(1, GAME_BORAD_WIDTH); x = random(1, GAME_BOARD_WIDTH);
y = random(1, GAME_BORAD_HEIGHT); y = random(1, GAME_BOARD_HEIGHT);
}while(!isAppleValid(x, y)); }while(!isAppleValid(x, y));
log("generateApple: ", {x: x, y: y});
return {x: x, y: y}; return {x: x, y: y};
} }
@@ -158,8 +157,8 @@ function isAppleValid(x, y){
function collisionTest(){ function collisionTest(){
//检测蛇有没有撞到墙上 //检测蛇有没有撞到墙上
var head = snake[0]; var head = snake[0];
if(head.x < 1 || head.x > GAME_BORAD_WIDTH if(head.x < 1 || head.x > GAME_BOARD_WIDTH
|| head.y < 1 || head.y > GAME_BORAD_HEIGHT){ || head.y < 1 || head.y > GAME_BOARD_HEIGHT){
gameOver(); gameOver();
return; return;
} }

File diff suppressed because one or more lines are too long

View File

@@ -275,7 +275,7 @@ module.exports = function (runtime, global) {
var args = arguments; var args = arguments;
global.__exitIfError__(function () { global.__exitIfError__(function () {
//不支持使用apply的原因是rhino会把参数中的primitive变成object //不支持使用apply的原因是rhino会把参数中的primitive变成object
functionApply(view.emit, args); functionApply(view, view.emit, args);
//view.emit.apply(view, args); //view.emit.apply(view, args);
}); });
} }
@@ -330,7 +330,7 @@ module.exports = function (runtime, global) {
var args = arguments; var args = arguments;
global.__exitIfError__(function () { global.__exitIfError__(function () {
//不支持使用apply的原因是rhino会把参数中的primitive变成object //不支持使用apply的原因是rhino会把参数中的primitive变成object
functionApply(list.emit, args); functionApply(list, list.emit, args);
//view.emit.apply(view, args); //view.emit.apply(view, args);
}); });
} }
@@ -354,21 +354,21 @@ module.exports = function (runtime, global) {
return e; return e;
} }
function functionApply(func, args) { function functionApply(obj, func, args) {
if (args.length == 0) if (args.length == 0)
return func(); return func.call(obj);
if (args.length == 1) if (args.length == 1)
return func(args[0]); return func.call(obj, args[0]);
if (args.length == 2) if (args.length == 2)
return func(args[0], args[1]); return func.call(obj, args[0], args[1]);
if (args.length == 3) if (args.length == 3)
return func(args[0], args[1], args[2]); return func.call(obj, args[0], args[1], args[2]);
if (args.length == 4) if (args.length == 4)
return func(args[0], args[1], args[2], args[3]); return func.call(obj, args[0], args[1], args[2], args[3]);
if (args.length == 5) if (args.length == 5)
return func(args[0], args[1], args[2], args[3], args[4]); return func.call(obj, args[0], args[1], args[2], args[3], args[4]);
if (args.length == 6) if (args.length == 6)
return func(args[0], args[1], args[2], args[3], args[4], args[5]); return func.call(obj, args[0], args[1], args[2], args[3], args[4], args[5]);
throw new Error("too many arguments: " + args.length); throw new Error("too many arguments: " + args.length);
} }

View File

@@ -77,7 +77,7 @@ public class ScriptCanvasView extends SurfaceView implements SurfaceHolder.Callb
canvas = holder.lockCanvas(); canvas = holder.lockCanvas();
scriptCanvas.setCanvas(canvas); scriptCanvas.setCanvas(canvas);
scriptCanvas.drawColor(Color.WHITE); scriptCanvas.drawColor(Color.WHITE);
emit("draw", scriptCanvas, this); emit("draw", scriptCanvas, ScriptCanvasView.this);
holder.unlockCanvasAndPost(canvas); holder.unlockCanvasAndPost(canvas);
canvas = null; canvas = null;
long dt = mTimePerDraw - (SystemClock.uptimeMillis() - time); long dt = mTimePerDraw - (SystemClock.uptimeMillis() - time);