Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

185
Views
¿Cómo hago un bucle a través de una matriz hacia atrás?

Estoy tratando de hacer una serpiente en el script Java usando p5.js y tener la serpiente como una matriz donde la cabeza es el primer índice y la cola cada punto después. Estoy tratando de recorrer la matriz hacia atrás para mover las cajas para que no terminen todas en un cuadrado, pero el resto del código dejó de funcionar cuando probé esto. Se supone que la cabeza debe bajar y esto funciona cuando paso normalmente con la cabeza en el otro extremo, pero necesito ir hacia atrás para poder agregar más a la matriz. aquí está mi código:

 function setup() { createCanvas(400, 400); } class SnakeBlock{ constructor(x, y, isHead){ this.x = x; this.y = y; this.isHead = isHead; } draw(){ fill("green"); noStroke(); rect(this.x * 40, this.y * 40, 40, 40) } move(snake, x){ if(this.isHead == true){ this.y += 1; } else{ this.y = snake[x - 1].y; this.x = snake[x - 1].x; } } } var length = 4; var moveCounter = 20; var snake = [new SnakeBlock(0, 0, true),new SnakeBlock(1, 0, false),new SnakeBlock(2, 0, false), new SnakeBlock(3, 0, false)]; function draw() { background(0); for(let x = length - 1; x > 0; x--){ snake[x].draw(); } if(moveCounter == 0){ for(let x = length - 1; x > 0; x--){ snake[x].move(snake, x); snake[x].draw(); } moveCounter = 20; } else{ moveCounter -= 1; } }

cuando lo tenía girando hacia el otro lado, la cabeza de serpiente se movería hacia abajo como se ve en la función move () en la clase de serpiente, pero cuando hago un bucle hacia atrás, no se mueve hacia abajo y simplemente permanece en su lugar. sí, invertí la matriz cuando hice un bucle en el otro sentido.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Simple, gira hacia el otro lado. En lugar de i++ use i-- :

 for(i = 0; i < array.length; i++){ // do something with array[i] } // you go backwards: for(i = array.length - 1; i >= 0; i--){ // do something with array[i] }
about 4 years ago · Juan Pablo Isaza Report

0

Opción 1

 var arr = [1, 2, 3, 4, 5]; for (var i = arr.length - 1; i >= 0; i--) { console.log(arr[i]); }

opcion 2

 var arr = [1, 2, 3, 4, 5]; arr.slice().reverse().forEach((item) => { console.log(item); });
about 4 years ago · Juan Pablo Isaza Report

0

Puede lograr esto usando el método Array.prototype.reduceRight()

 const arr = [1,2,3,4,5]; arr.reduceRight((_, el) => { console.log(el); // do stuff return el; }, null)

Más información sobre reduceRight - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduceRight

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!