我似乎无法弄清楚错误是什么。我尝试在设置之前声明更新,但我仍然无法在另一个实例方法上调用实例方法。
我相信这应该有效。但我可能是wrong。现在我知道' this
'可能会在范围发生变化时发生变化,我无论如何都要引用该实例来调用我的方法!
我怎么能绕过这个?
function MainGameScene(renderer , screenSize)
{
PIXI.loader
.add("_assets/textures/sprites.json")
.load(this.setup);
}
MainGameScene.prototype.update = function()
{
var now = Date.now();
var delta = (now - this.mLastCalledTime);
this.mLastCalledTime = now;
this.mElapsed += (delta / 1000);
this.mSpaceShip.getSprite().x += this.mSpaceShip.getVelocity().x;
this.mSpaceShip.getSprite().y += this.mSpaceShip.getVelocity().y;
if(this.mCurrentScore != this.mSpaceShip.getHits())
{
this.mCurrentScore = this.mSpaceShip.getHits();
scoreText.setText(this.mCurrentScore);
}
if(this.mElapsed >= MAX_SPAWN_RATE)
{
this.generateEnemy();
this.mElapsed = 0.0;
}
for(var e in this.mEnemyShipsArray)
{
var index = parseInt(e);
var enemy = this.mEnemyShipsArray[index];
enemy.getSprite().y += enemy.getVelocity().y;
if(enemy.getSprite().y > this.mScreenSize.height + 50 || enemy.isMarkedDestroy())
this.mEnemyShipsArray.splice(index , 1);
}
this.mRenderer.render(this.mStage);
requestAnimationFrame(this.update);
}
MainGameScene.prototype.setup = function()
{
this.mRenderer = renderer;
this.mScreenSize = screenSize;
this.MAX_SPAWN_RATE = 3.0;
this.SPACESHIP_SPPED = 5.0;
this.ENEMYSHIP_SPEED = 3.8;
this.mSpaceShip = {};
this.mEnemyShip = {};
this.mLastCalledTime = Date.now();
this.mElapsed = 0.0;
this.mStyle =
{
font : 'bold italic 36px Arial'
, fill : '#F7EDCA'
, stroke : '#4a1850'
, strokeThickness : 5
}
this.mCurrentScore = 0;
this.mScoreText = new PIXI.Text(this.mCurrentScore , this.mStyle);
this.mScoreText.x = screenSize.width / 2.0;
this.mScoreText.y = 30.0;
this.mEnemyShipsArray = new Array();
this.mStage = new PIXI.Container();
this.mSpaceShip = new SpaceShip(this.mStage , "spaceship.png");
this.mSpaceShip.setPixelPosition({x : 200 , y : 550});
this.mSpaceShip.perceiveEnemyShips(this.mEnemyShipsArray);
this.mStage.addChild(this.mSpaceShip.getSprite());
this.mStage.addChild(this.mSpaceShip.getSprite());
document.onkeydown = function()
{
var e = e || window.event;
if(e.keyCode == '38') // up
{
//if((spaceship.getSprite().y + (spaceship.getSprite().height / 2.0)) <= screenSize.height)
this.mSpaceShip.setVelocityY(-SPACESHIP_SPEED);
}
if(e.keyCode == '40') // down
{
//if((spaceship.getSprite().y - (spaceship.getSprite().height / 2.0)) >= 0)
this.mSpaceShip.setVelocityY(SPACESHIP_SPEED);
}
if(e.keyCode == '37') // left
{
//if((spaceship.getSprite().x - (spaceship.getSprite().width / 2.0)) >= 0)
this.mSpaceShip.setVelocityX(-SPACESHIP_SPEED);
}
if(e.keyCode == '39') // right
{
//if((spaceship.getSprite().x + (spaceship.getSprite().width / 2.0)) <= screenSize.width)
this.mSpaceShip.setVelocityX(SPACESHIP_SPEED);
}
}
document.onkeyup = function()
{
var e = e || window.event;
if(e.keyCode == '38' || e.keyCode == '40') // up and down
this.mSpaceShip.setVelocityY(0);
if(e.keyCode == '37' || e.keyCode == '39') // left and right
this.mSpaceShip.setVelocityX(0);
}
self.update();
}
我是JavaScript新手,我无法使用这种脚本语言来处理实例。
我会感激一些帮助!
答案 0 :(得分:0)
你应该采用这种方法将它与嵌套函数一起使用:
function LoggedOrNot()
{
@if (Page.User.Identity.IsAuthenticated)
{
<text>var x = document.cookie;
document.getElementByID("signupbutton").innerHTML = x;</text>
}
}
答案 1 :(得分:0)
似乎我的问题与从另一个实例方法调用实例方法更相关。
我的&#39;这个&#39;指针始终指向我正在使用的框架的Loader类。我似乎无法在SO中找到链接,但我能够通过这样做解决我的问题:
var loader = new PIXI.loaders.Loader();
loader.add("_assets/textures/sprites.json");
loader.once("complete" , this.setup.bind(this));
loader.load();
问题是,我必须将我的方法调用绑定到this
。 this
总是根据功能范围而变化。
也许我会切换到另一种JavaScript,比如使用CoffeeScript的TypeScript,这样我就可以轻松完成这些工作。
感谢您的帮助!