每当我打电话给moveAvatar()
时,this.vx
和this.vy
都是不确定的。
如何?
它在普通JS应用程序中的工作方式:
游戏开始:设置一个间隔,反复调用draw()
,在画布上绘制。我为为什么使用打字稿无法使用而感到困惑。
vx
和vy
均已定义并分配了值。您甚至可以将它们打印出来...
我确实从一个运行良好的纯JS应用程序中移植了此代码。
我感觉到它与角度如何对待间隔有关。请告诉我!!谢谢!
import {
Component,
OnInit
} from '@angular/core';
@Component({
selector: 'app-avoid',
templateUrl: './avoid.component.html',
styleUrls: ['./avoid.component.css']
})
export class AvoidComponent implements OnInit {
avatarImage;
enemy;
x = 30;
y = 30;
_x = 300;
_y = 300;
score = 0;
vx: number = 0;
vy: number = 0;
scoreTracker;
gameCanvas;
frames;
gameStatus = false;
hud = {
score: 0,
left: 0,
right: 0,
up: 0,
down: 0
};
backstage;
time = 0;
level = 1;
enemySpeed = 0.5;
levelTracker;
constructor() {
}
ngOnInit() {
let body = document.querySelector('body');
body.addEventListener("keyup", this.moveAvatar);
this.gameCanvas = document.getElementById("gameCanvas");
this.scoreTracker = document.getElementById("score");
this.levelTracker = document.getElementById('level');
this.enemy = this.getEnemy();
this.avatarImage = this.getAvatar();
}
levelUp() {
this.level++;
this.enemySpeed += 0.25;
this.levelTracker.innerText = this.level.toString();
}
startCanvas() {
this.backstage = document.createElement("canvas");
this.backstage.width = this.gameCanvas.width;
this.backstage.height = this.gameCanvas.height;
if (this.frames != null) {
this.gameOver();
}
this.backstage.getContext("2d", {
alpha: false
}).drawImage(this.avatarImage, this.x, this.y);
this.backstage.getContext("2d", {
alpha: false
}).drawImage(this.enemy, this._x, this._y)
this.gameCanvas.getContext("2d", {
alpha: false
}).drawImage(this.backstage, 0, 0);
this.time = 0;
const that = this;
this.frames = setInterval(() => {
that.Draw();
}, 100);
}
getAvatar() {
var avatarImage = new Image(30, 30);
avatarImage.src = "../assets/images/avatar.png";
return avatarImage;
}
getEnemy() {
var enemy = new Image();
enemy.src = "../assets/images/enemy.png"
return enemy;
}
moveAvatar(key) {
switch (key.keyCode) {
case 37:
this.vx--;
break;
case 38:
this.vy--;
break;
case 39:
this.vx++;
break;
case 40:
this.vy++;
break;
}
}
Follow() {
if (this.x > this._x) {
this._x += this.enemySpeed;
} else if (this.x < this._x) {
this._x -= this.enemySpeed;
}
if (this.y > this._y) {
this._y += this.enemySpeed;
} else if (this.y < this._y) {
this._y -= this.enemySpeed;
}
}
Draw() {
this.time++;
this.Accelerate();
this.Follow();
if (this.x < 0 || this.y < 0 || this.y > 520 || this.x > 770) {
this.gameOver();
return;
}
this.score += 1;
this.scoreTracker.value = this.score;
this.backstage.width += 0;
this.gameCanvas.width += 0;
this.backstage.getContext("2d", {
alpha: false
}).drawImage(this.avatarImage, this.x, this.y);
this.backstage.getContext("2d", {
alpha: false
}).drawImage(this.enemy, this._x, this._y);
this.gameCanvas.getContext("2d", {
alpha: false
}).drawImage(this.backstage, 0, 0);
if (this.x <= this._x + 20 && this.x >= this._x - 20) {
if (this.y <= this._y + 20 && this.y >= this._y - 20) {
this.gameOver();
}
}
if (this.score % 500 === 0) {
this.levelUp();
}
}
Accelerate() {
this.x += this.vx;
this.y += this.vy;
}
gameOver() {
clearInterval(this.frames);
this.x = 30;
this.y = 30;
this._x = 300;
this._y = 300;
this.score = 0;
this.scoreTracker.value = 0;
this.level = 1;
this.enemySpeed = 0.5;
this.levelTracker.innerText = "1";
}
}
答案 0 :(得分:3)
这样称呼
body.addEventListener('keyup', this.moveAvatar.bind(this));
(OR)
const myObj = [{id: 15,children: [{id: 9,children: [{id: 4,children: []},{id: 1,children: []}]},{id: 4,children: [{id: 35,children: [{id: 12,children: []},{id: 8,children: []}]},{id: 30,children: [],}]},]},{id: 2,children: [{id: 9,children: []},{id: 3,children: []},]}];
const deepSortById = arr => (arr.forEach(a => a.children && deepSortById(a.children)), arr.sort((a, b) => a.id - b.id));
const result = deepSortById(myObj);
console.log(result);
答案 1 :(得分:1)
除了上述正确答案之外,您还可以使用Renderer模块来实现相同的效果。 Uncaught Error: Template parse errors:
Can't bind to 'files' since it isn't a known property of 'ngfFormData'. ("
<ngfFormData [ERROR ->][files]="files" postName="file" [(FormData)]="sendableFormData"></ngfFormData>
<ngfUploadS"): ng:///UploadModule/UploadComponent.html@4:19
Can't bind to 'FormData' since it isn't a known property of 'ngfFormData'. ("
<ngfFormData [files]="files" postName="file" [ERROR ->][(FormData)]="sendableFormData"></ngfFormData>
<ngfUploadStatus [(percent)]="progress" [ht"): ng:///UploadModule/UploadComponent.html@4:51
'ngfFormData' is not a known element:
1. If 'ngfFormData' is an Angular component, then verify that it is part of this module.
2. To allow any element add 'NO_ERRORS_SCHEMA' to the '@NgModule.schemas' of this component. ("
[ERROR ->]<ngfFormData [files]="files" postName="file" [(FormData)]="sendableFormData"></ngfFormData>
"): ng:///UploadModule/UploadComponent.html@4:6
Can't bind to 'percent' since it isn't a known property of 'ngfUploadStatus'. ("="files" postName="file" [(FormData)]="sendableFormData"></ngfFormData>
<ngfUploadStatus [ERROR ->][(percent)]="progress" [httpEvent]="httpEvent"></ngfUploadStatus>
;无需手动添加侦听器,而是让角度创建和销毁侦听器。