【发布时间】:2021-01-02 20:50:13
【问题描述】:
我正在使用 Nextjs、React hooks 和 svg 制作一个非常基本的游戏引擎。
我想检测两个 svg 之间的冲突,但我不知道该怎么做。
尝试此语法后
circle.isPointInFill(new DOMPoint(10, 10))
在看到 so-question 后,我转而使用 createSVGPoint
我想使用 React 的 useRef 来获取对 svg 的引用(我真的不想使用 JQuery),但我似乎无法让 createSVGPoint 函数工作,所以我可以使用 isPointInFill 检查点是否为在 svg 中。
我在这里similar problem in Angular 看到了类似的问题,但我不知道如何使用 useRef 来做类似的事情?
document.createElementNS('http://www.w3.org/2000/svg', 'svg');
这是我的 index.js
import Head from 'next/head'
import { useState, useEffect, useRef } from 'react'
import {useKeyPress} from '../hooks/useKeyPress'
import Circle from '../components/svgs/circle'
import Room from '../components/svgs/room'
import utilStyles from '../styles/utils.module.css'
export default function Home() {
const [ currentXPos, setCurrentXPos ] = useState(500)
const [ currentYPos, setCurrentYPos ] = useState(500)
const [ colourIndex, setColourIndex ] = useState(0)
const circle = useRef()
const circleColours = [
"green",
"orange",
"red",
"blue"
]
const upPress = useKeyPress('w');
const rightPress = useKeyPress('d');
const leftPress = useKeyPress('a');
const downPress = useKeyPress('s');
const setNewXPos = (circle, changeXPos) => {
let point = circle.createSVGPoint();
point.x = 40;
point.y = 32;
let _newColourIndex = (colourIndex + 1) % 4
let _newXPos = currentXPos + changeXPos
if (_newXPos < 50) {
_newXPos = currentXPos
} else if (_newXPos > 950) {
_newXPos = currentXPos
}
if (_newXPos % 150 === 0) {
setColourIndex(_newColourIndex)
}
console.log("CHECK IF POINT IN CIRCLE REF", circle.isPointInFill(point))
setCurrentXPos(_newXPos)
}
const setNewYPos = (changeYPos) => {
let _newColourIndex = (colourIndex + 1) % 4
let _newYPos = currentYPos + changeYPos
if (_newYPos < 50) {
_newYPos = currentYPos
} else if (_newYPos > 800) {
_newYPos = currentYPos
}
if (_newYPos % 150 === 0) {
setColourIndex(_newColourIndex)
}
setCurrentYPos(_newYPos)
}
useEffect(() => {
console.log("CIRCLE", circle.current)
if (upPress) {
console.log("UP!")
setNewYPos(-1)
}
if (rightPress) {
console.log("RIGHT!")
setNewXPos(circle, 1)
}
if (leftPress) {
console.log("LEFT!")
setNewXPos(circle, -1)
}
if (downPress) {
console.log("DOWN!")
setNewYPos(1)
}
}, [
currentXPos,
currentYPos,
circleColours,
circle,
upPress,
rightPress,
leftPress,
downPress
])
return (
<div className="container">
<Head>
<title>Game Engine Thing</title>
</Head>
<main>
<h1 className="title">
Very Basic React Game Engine {currentXPos} {currentYPos}
</h1>
<Room />
{currentXPos && <section className={`${utilStyles.gameEnv}`}>
<svg ref={circle} width="1000" height="1000">
<Circle
xPos={currentXPos}
yPos={currentYPos}
fill={circleColours[colourIndex]}
/>
</svg>
</section>}
</main>
</div>
)
}
完整代码在complete code
【问题讨论】:
-
createSVGPoint 是
标签: svg next.js collision-detection