fill array

PT_PT_
edited June 2014 in Programming Questions

hi, another problem: you can i change the first 10 variables of an array? i've made an array with the x- and y-coördinate and the length + width of 10 cars, so there are 40 variables in the array. but in my game, these variables change. how can the array change together?

Tagged:

Answers

  • Be more specific, please: how should this variables change? Anyway, as GoToLoop suggested, you should consider the idea of using classes instead of arrays.

  • edited August 2014

    Hello,

    without following gotoloops good advice:

    You should post some code. ;-)

    Anyway.

    When you have an array x you can say

    x[3] = x[3] + 5; 
    

    so the x-position of car #3 would change by 5 (it drives right).

    to make this easier you can write a for loop and say

    (not tested)

    for (i=0; i<x.length; i++) {
    
         x[i] = x[i] + 5;
    
    } 
    

    Thus you change all cars in 2 lines. Nice, right? You use the i value as an index.

    When you want to have the car's speed be different, make a new array speed with holds the speeds btw. and say

    (pseudo-code)

    for i.........................
    
        x[i] = x[i] + speed[i]; 
    

    Also when you display the cars, I hope you use the for-loop... ;-)

    Chrisir ;-)

Sign In or Register to comment.