我正在使用p5.play创建一个类似于p5.Js中的战斗城市的游戏。现在我正准备射出一颗子弹。我已经使用箭头键进行了精灵移动。我还做了一个函数,在这个函数中,无论坦克在哪里,子弹都会产生。但是子弹不会从坦克里产生也不会发射。它是从我画布的左下角发射的。当我检查我的坦克的坐标时,即使精灵在移动,它也不会改变。你知道为什么和如何解决这个问题吗?
let tankImg;
let tankx = 50
let tanky = 570
let bullets = []
let bulletSpeed = 5
function preload(){
tankImg = loadImage('tankk.jpg')
blockImg = loadImage('wall.jpg')
bulletImg = loadImage('bullet.jpg')
}
let tank;
let block;
function setup() {
createCanvas(600, 600);
//tank sprite
tank = createSprite(tankx, tanky, 70,34)
tank.addImage(tankImg)
//wall sprite
left_wall = createSprite(0,0,10,1200)
up_wall = createSprite(0,0,1200,10)
right_wall = createSprite(600,0,-10,1200)
down_wall = createSprite(600,600, 1200,20)
//barriers
//first phse
block = createSprite(150,30, 70,40)
block.addImage(blockImg)
block1 = createSprite(150,75, 70,40)
block1.addImage(blockImg)
block2 = createSprite(150,120, 70,40)
block2.addImage(blockImg)
block3 = createSprite(150,165, 70,40)
block3.addImage(blockImg)
block4 = createSprite(150,210, 70,40)
block4.addImage(blockImg)
block5 = createSprite(150,255, 70,40)
block5.addImage(blockImg)
block6 = createSprite(150,300, 70,40)
block6.addImage(blockImg)
block7 = createSprite(150,345, 70,40)
block7.addImage(blockImg)
//second Phase
block8 = createSprite(300,570, 70,40)
block8.addImage(blockImg)
block9 = createSprite(300,525, 70,40)
block9.addImage(blockImg)
block10 = createSprite(300,480, 70,40)
block10.addImage(blockImg)
block11 = createSprite(300,30, 70,40)
block11.addImage(blockImg)
block12 = createSprite(300,75, 70,40)
block12.addImage(blockImg)
block13 = createSprite(300,120, 70,40)
block13.addImage(blockImg)
block14 = createSprite(300,165, 70,40)
block14.addImage(blockImg)
block15 = createSprite(300,210, 70,40)
block15.addImage(blockImg)
//third phase
block16 = createSprite(450,30, 70,40)
block16.addImage(blockImg)
block17 = createSprite(450,75, 70,40)
block17.addImage(blockImg)
block18 = createSprite(450,120, 70,40)
block18.addImage(blockImg)
block19 = createSprite(450,165, 70,40)
block19.addImage(blockImg)
block20 = createSprite(450,210, 70,40)
block20.addImage(blockImg)
block21= createSprite(450,255, 70,40)
block21.addImage(blockImg)
block22 = createSprite(450,300, 70,40)
block22.addImage(blockImg)
block23 = createSprite(450,345, 70,40)
block23.addImage(blockImg)
}
function draw() {
background(100, 100,250);
//rotation of the tank
tank_func()
drawSprites()
}
//tank function
function tank_func(){
if(keyIsPressed && keyCode == RIGHT_ARROW){
tank.rotation++
tank.setVelocity(1, 0)
}if(keyIsPressed && keyCode == LEFT_ARROW){
tank.rotation--
tank.setVelocity(-1, 0)
}
if(keyIsDown(UP_ARROW)){
tank.setVelocity(0,-1)
}if(keyIsDown(DOWN_ARROW)){
tank.setVelocity(0,1)
}
//boundaries
left_wall.collide(tank, bounce_off)
right_wall.collide(tank, bounce_off)
up_wall.collide(tank, bounce_off)
down_wall.collide(tank, bounce_off)
//first phase collision
block.collide(tank, bounce_off)
block1.collide(tank, bounce_off)
block2.collide(tank, bounce_off)
block3.collide(tank, bounce_off)
block4.collide(tank, bounce_off)
block5.collide(tank, bounce_off)
block6.collide(tank, bounce_off)
block7.collide(tank, bounce_off)
//second phase collision
block8.collide(tank, bounce_off)
block9.collide(tank, bounce_off)
block10.collide(tank, bounce_off)
block11.collide(tank, bounce_off)
block12.collide(tank, bounce_off)
block13.collide(tank, bounce_off)
block14.collide(tank, bounce_off)
block15.collide(tank, bounce_off)
//third phase collision
block16.collide(tank, bounce_off)
block17.collide(tank, bounce_off)
block18.collide(tank, bounce_off)
block19.collide(tank, bounce_off)
block20.collide(tank, bounce_off)
block21.collide(tank, bounce_off)
block22.collide(tank, bounce_off)
block23.collide(tank, bounce_off)
if (bullets.length > 0) {
for (var i = 0; i < bullets.length; i++) {
bullets[i].render(200, 200, 0);
if (bullets[i].x < 0 || bullets[i].x > width ||
bullets[i].y < 0 || bullets[i].y > height)
bullets.splice(i, 1)
}
}
}
function keyPressed(){
if(keyCode == 32){
noStroke()
fill(250,0,0)
bullets.push(new Bullet(tankx, tanky, 50,
bulletSpeed));
console.log(bullets)
}
}
function bounce_off(){
tank.setVelocity(0,0)
block.setVelocity(0,0)
}下面是我的bullet函数:
function Bullet (tx, ty, ts, tspeed){
this.x = tx;
this.y = ty;
this.s = ts;
this.speed = tspeed;
this.render = function(r,g,b) {
fill(r,g,b);
rect(this.x, this.y, this.s, this.speed);
this.x = this.x - this.speed
}
}如果你还想检查真实的文件,这里是网页编辑器的链接:https://editor.p5js.org/somr_pel/sketches/ACXgrz0gk
发布于 2021-01-12 19:30:03
在函数keyPressed()中,必须将tankx和tanky替换为tank.position.x和tank.position.y。
它不会更新,因为您没有更新tankx和tanky变量,而是更新了tank对象,并且如tank的console.log中所示,position属性已更新。所以更容易使用它。
function keyPressed(){
if(keyCode == 32){
noStroke()
fill(250,0,0)
console.log(tank);
bullets.push(new Bullet(tank.position.x, tank.position.y, 50,
bulletSpeed));
console.log(bullets)
}
}https://stackoverflow.com/questions/65682809
复制相似问题