现在,我有一个蛇游戏,它在代码底部使用React.renderComponent(SnakeGame(null, null ), document.body);
。它引用SnakeGame
作为包含游戏逻辑的变量。最终,我想为蛇添加图像。我正在通过将本地图像marble1.png
放在render函数中进行测试,但这会导致其他所有内容都无法渲染,并且看不到该图像。我想知道如何获取它以便React渲染所有内容?
这是代码的主要部分:
var SnakeGame = React.createClass({displayName: 'SnakeGame',
getInitialState: function() {
var start = this.props.startIndex || 21;
var snake = [start], board = []; board[start] = BODY;
return {
snake: snake,
board: board,
growth: 0,
paused: true,
gameOver: false,
direction: KEYS.right
}
},
componentDidMount: function() {
this._resume();
},
_reset: React.autoBind(function() {
this.setState(this.getInitialState());
this._resume();
}),
_pause: React.autoBind(function() {
if (this.state.gameOver || this.state.paused) { return; }
this.setState({paused: true});
}),
_resume: React.autoBind(function() {
if (this.state.gameOver || !this.state.paused) { return; }
this.setState({paused: false});
this.refs.board.getDOMNode().focus();
this._tick();
}),
_tick: React.autoBind(function() {
if (this.state.paused) { return; }
var snake = this.state.snake;
var board = this.state.board;
var growth = this.state.growth;
var direction = this.state.direction;
var numRows = this.props.numRows || 20;
var numCols = this.props.numCols || 20;
var head = getNextIndex(snake[0], direction, numRows, numCols);
if (snake.indexOf(head) != -1) {
this.setState({gameOver: true});
return;
}
var needsFood = board[head] == FOOD || snake.length == 1;
if (needsFood) {
var ii, numCells = numRows * numCols;
do { ii = Math.floor(Math.random() * numCells); } while (board[ii]);
board[ii] = FOOD;
speedUp = true;
currTicks=50;
ticksWithSpeed = 20;
growth += 2;
} else if (growth) {
growth -= 1;
} else {
board[snake.pop()] = null;
}
snake.unshift(head);
board[head] = BODY;
if (this._nextDirection) {
direction = this._nextDirection;
this._nextDirection = null;
}
this.setState({
snake: snake,
board: board,
growth: growth,
direction: direction,
speedUp: speedUp
});
var currSpeed = 100;
if(this.state.speedUp){
currSpeed = 40;
currTicks--;
console.log("number of ticks is "+currTicks);
}
if(currTicks == 0){
speedUp = false;
currSpeed = 100;
currTicks=50;
}
setTimeout(this._tick, currSpeed);
}),
_handleKey: React.autoBind(function(event) {
var direction = event.nativeEvent.keyCode;
var difference = Math.abs(this.state.direction - direction);
// if key is invalid, or the same, or in the opposite direction, ignore it
if (DIRS[direction] && difference !== 0 && difference !== 2) {
this._nextDirection = direction;
}
}),
render: function() {
var cells = [];
var numRows = this.props.numRows || 20;
var numCols = this.props.numCols || 20;
var cellSize = this.props.cellSize || 30;
for (var row = 0; row < numRows; row++) {
for (var col = 0; col < numCols; col++) {
var code = this.state.board[numCols * row + col];
var type = code == BODY ? 'body' : code == FOOD ? 'food' : 'null';
cells.push(React.DOM.div( {className:type + '-cell'}, null ));
}
}
return (
React.DOM.div( {className:"snake-game"}, [
React.DOM.h1( {className:"snake-score"}, ["Length: ", this.state.snake.length]),
React.DOM.div(
{ref:"board",
className:'snake-board' + (this.state.gameOver ? ' game-over' : ''),
tabIndex:0,
onBlur:this._pause,
onFocus:this._resume,
onKeyDown:this._handleKey,
style:{width: numCols * cellSize, height: numRows * cellSize}},
cells
),
React.DOM.div( {className:"snake-controls"}, [
this.state.paused ? React.DOM.button( {onClick:this._resume}, "Resume") : null,
this.state.gameOver ? React.DOM.button( {onClick:this._reset}, "New Game") : null
]),
React.DOM.div({className:"marble"},[
//<img src={require('marble1.png')} alt="" />
])
])
);
}
});
function getNextIndex(head, direction, numRows, numCols) {
// translate index into x/y coords to make math easier
var x = head % numCols;
var y = Math.floor(head / numCols);
// move forward one step in the correct direction, wrapping if needed
switch (direction) {
case KEYS.up: y = y <= 0 ? numRows - 1 : y - 1; break;
case KEYS.down: y = y >= numRows - 1 ? 0 : y + 1; break;
case KEYS.left: x = x <= 0 ? numCols - 1 : x - 1; break;
case KEYS.right: x = x >= numCols - 1 ? 0 : x + 1; break;
default: return;
}
// translate new x/y coords back into array index
return (numCols * y) + x;
}
React.renderComponent(SnakeGame(null, null ), document.body);
我也觉得那部分:
React.DOM.div(
{ref:"board",
className:'snake-board' + (this.state.gameOver ? ' game-over' : ''),
tabIndex:0,
onBlur:this._pause,
onFocus:this._resume,
onKeyDown:this._handleKey,
style:{width: numCols * cellSize, height: numRows * cellSize}},
cells
)
正在引起问题,因为我已经说明了DOM在可以处理图像之前要呈现的选项。