2017-10-17 47 views
0

主にキャンバスを反応させるためにサイズを変更していたので、バニラのJavaScript PongゲームをPixiJSに書き直そうとしました。PixiJSの助けを借りてバニラJSポンを書き直す

サイズ変更がPixiJSと正常に動作しますが、すべてのオブジェクトは、末尾のパスを持っているために開始しました:Fiddle1私は考え出し

その更新時に新しいインスタンスを追加しただけではなく位置を更新するので、私は最初からそれを手直ししようとしたためです再び。プレイヤーの動きはこれまでのところ動作しますが、私はボールを動かすことができません...私はちょっと失われ、学ぶ意志があります。ヘルプは大変感謝しています...ありがとう!

Fiddle2

import 'pixi.js'; 

// define gamne variables 
const appWidth = window.innerWidth; 
const appHeight = window.innerHeight; 
const paddleWidth = 150; 
const paddleHeight = 30; 
const ballSize = 15; 
const halfBall = ballSize/2; 
const appWidthHalf = appWidth/2; 
const appHeightHalf = appHeight/2; 
const paddleWidthHalf = paddleWidth/2; 
const pongColor = 0x57dfbf; 
const bgColor = 0x282625; 
const computerPositionX = appWidthHalf - paddleWidthHalf; 
const computerPositionY = 50; 
const playerPositionX = computerPositionX; 
const playerPositionY = appHeight - computerPositionY - paddleHeight; 
const ballPositionX = appWidthHalf - halfBall; 
const ballPositionY = appHeightHalf - halfBall; 
const playerSpeed = 4; 
const computerSpeed = 4; 
const ballSpeed = 3; 

// Setup the ticker and the root stage PIXI.Container. 
const app = new PIXI.Application(appWidth, appHeight, { 
    antialias: false, 
    backgroundColor: bgColor, 
    transparent: false, 
    resolution: 1, 
}); 

// append app to body 
document.body.appendChild(app.view); 

// create graphic elements 
const player = new PIXI.Graphics(); 
const computer = new PIXI.Graphics(); 
const ball = new PIXI.Graphics(); 

// Player 
player 
    .beginFill(pongColor) 
    .drawRect(playerPositionX, playerPositionY, paddleWidth, paddleHeight) 
    .endFill(); 

// Computer 
computer 
    .beginFill(pongColor) 
    .drawRect(computerPositionX, computerPositionY, paddleWidth, paddleHeight) 
    .endFill(); 

// Ball 
ball 
    .beginFill(pongColor) 
    .drawRect(ballPositionX, ballPositionY, ballSize, ballSize) 
    .endFill(); 

// Player Movement 
player.update = function() { 
    for (const key in keysDown) { 
    const value = Number(key); 
    if (value === 37) { 
     player.move(-playerSpeed, 0); 
    } else if (value === 39) { 
     player.move(playerSpeed, 0); 
    } else { 
     player.move(0, 0); 
    } 
    } 
}; 

player.move = function (x, y) { 
    this.x += x; 
    this.y += y; 
    this.x_speed = x; 
    this.y_speed = y; 
    if (this.x < -appWidthHalf + paddleWidthHalf) { 
    this.x = -appWidthHalf + paddleWidthHalf; 
    this.x_speed = 0; 
    } else if (this.x + this.width - paddleWidthHalf > appWidthHalf) { 
    this.x = appWidthHalf - this.width + paddleWidthHalf; 
    this.x_speed = 0; 
    } 
}; 

// computer Movement 
// eslint-disable-next-line 
computer.update = function(ball) { 
    const x_pos = ball.x; 
    let diff = -(computer.x + paddleWidthHalf - x_pos); 
    if (diff < 0 && diff < -computerSpeed) { 
    diff = -ballSize; 
    } else if (diff > 0 && diff > computerSpeed) { 
    diff = ballSize; 
    } 
    computer.position.set(diff, 0); 
    if (computer.x < 0) { 
    computer.x = 0; 
    } else if (computer.x + paddleWidthHalf > appWidth) { 
    computer.x = appWidth - paddleWidthHalf; 
    } 
}; 

// Ball Movement 
ball.update = function (paddle1, paddle2) { 
    this.x += this.x_speed; 
    this.y += this.y_speed; 
    const top_x = this.x - ballSize; 
    const top_y = this.y - ballSize; 
    const bottom_x = this.x + ballSize; 
    const bottom_y = this.y + ballSize; 
    if (this.x - ballSize < 0) { 
    this.x = ballSize; 
    this.x_speed = -this.x_speed; 
    } else if (this.x + ballSize > appWidth) { 
    this.x = appWidth - ballSize; 
    this.x_speed = -this.x_speed; 
    } 

    if (this.y < 0 || this.y > appHeight) { 
    this.x_speed = 0; 
    this.y_speed = ballSpeed; 
    this.x = appWidthHalf; 
    this.y = appHeightHalf; 
    } 

    if (top_y > appHeightHalf) { 
    if (
     top_y < paddle1.y + paddle1.height && 
     bottom_y > paddle1.y && 
     top_x < paddle1.x + paddle1.width && 
     bottom_x > paddle1.x 
    ) { 
     this.y_speed = -ballSpeed; 
     this.x_speed += paddle1.x_speed/2; 
     this.y += this.y_speed; 
    } 
    } else if (
    top_y < paddle2.y + paddle2.height && 
    bottom_y > paddle2.y && 
    top_x < paddle2.x + paddle2.width && 
    bottom_x > paddle2.x 
) { 
    this.y_speed = ballSpeed; 
    this.x_speed += paddle2.x_speed/2; 
    this.y += this.y_speed; 
    } 
}; 

// controls 
const keysDown = {}; 

window.addEventListener('keydown', (event) => { 
    keysDown[event.keyCode] = true; 
}); 

window.addEventListener('keyup', (event) => { 
    delete keysDown[event.keyCode]; 
}); 

// update function 
function update() { 
    player.update(); 
    computer.update(ball); 
    ball.update(player, computer); 
} 

// append childs to app 
app.stage.addChild(player); 
app.stage.addChild(computer); 
app.stage.addChild(ball); 

// game loop 
app.ticker.add(update); 

答えて

0

フィドル1は、あなたがそこにball.xとyを変えていなかったとして間違いなく、問題があったが、代わりにグラフィックを変更するオブジェクト、fiddle2にあなたが 良い仕事をしていた、ように見えましたそれ。 x座標とy座標の設定とボールの移動に関して、コードに論理的なエラーがあります。

したがって、fiddle2のball.update関数の中にconsole.log(this.x、this.y)を追加すると、NaN値がわかります。

あなたと初期値を設定した場合、ボールが動いて表示するには:

ball.x_speed = 0; 
ball.y_speed = 0; 

そして

paddle.x_speed 
paddle.y_speed 

にあなたのボールが(少なくとも移動参照

paddle.x 
paddle.y 

衝突ロジックは機能しませんが)(下記のフィディッドの変更例) https://jsfiddle.net/4L6zbd01/

関連する問題