1 回答
TA贡献1836条经验 获得超13个赞
我会这样做:
import quackSound from "./music/Duck-quack.mp3";
class MallardDuck extends Duck {
constructor(props) {
super();
this.state = {
canFly: false,
canQuack: false,
round:0,
quackSound: new Audio(quackSound),
};
this.intervalRef=null;
}
quack = () => {
const quack = new QuackSound();
return quack.quack();
};
fly = () => {
const fly = new FlyWings();
return fly.fly();
};
render() {
return (
<div className="duck">
<img
className={`image ${this.state.canFly ? "canFly" : ""}`}
src={mallardDuck}
alt="mallardDuck"
onAnimationEnd={() => {
this.setState({ canFly: false });
}}
/>
<Button
className="eventButton"
onClick={(event) => {
event.preventDefault();
this.setState({ canFly: true });
}}
>
Fly
</Button>
<Button
className="eventButton"
onClick={(event) => {
event.preventDefault();
//you can tweek interval duration for your liking , for now its set to pay every .3 seconds (300 ms)
const myInterval=setInterval(()=>{
if(this.state.round > 3) clearInterval(myInterval);
this.setState({ canQuack: true });
this.setState({ round: this.state.round+1 });
this.state.quackSound.play(); // Here !!!!!!},300)
}}
>
Quack
</Button>
{this.state.canQuack ? this.quack() : null}
{this.state.canFly ? this.fly() : null}
</div>
);
}
}
添加回答
举报