This site requires JavaScript, please enable it in your browser!
Greenfoot back
Lambda
Lambda wrote ...

2015/2/26

What's wrong? Object away from another

Lambda Lambda

2015/2/26

#
Hey, I've made following code, that should generate objects seperated from each other. As you can see, I made it to be minimum 200pixels away. But it seems like it means nothing the java and usually these are generated one on the another, and if not, then really close to each other. What am i doing wrong?
    public void generateBeam()
    {
        beamXposition = new int[10];

        if (beamCount == 0)
        {
            addObject(new Beam(), 50, 500);
            beamXposition[0] = 50;
            beamCount++;
        }
        else    
        {   
            int newXpos = beamXposition[beamCount-1] + getRandomNumber(200, 300);
            beamXposition[beamCount] = newXpos;
            addObject(new Beam(), newXpos, 500);
            beamCount++;
        }
    }

    public int getRandomNumber(int start, int end)
    {
       int number = Greenfoot.getRandomNumber(end-start);
       return number+start;
    }
davmac davmac

2015/2/26

#
line 3 gets executed each time the generateBeam method is called - so, you're resetting the array contents every time the method is called.
Lambda Lambda

2015/2/26

#
Damn, I just found the problem before refreshing the thread page. Shame on me, works now. Thanks! :P
You need to login to post a reply.