samedi 2 janvier 2016

random and time adequacy

I try to do my own version of "fruit ninja" for training based on this version

http://ift.tt/1MNu1rj

I have done some minor changes.

What I want to do is to affect different scores to the object in the "enum" in fruittype.

So

I add this function(in the aim to retrieve the current random value):

public static int currentrandom() {


    return random.nextInt(FruitType2.values().length );
}

and I add

   if (FruitType2.currentrandom()<=9)
        {score++;}
        else
        {score=score-5;}

at the end of FruitProjectileManager.

Complete code for FruitProjectileManager:

       package fruitninja;

      import android.content.Context;
         import android.content.res.Resources;
     import android.graphics.Bitmap;
     import android.graphics.BitmapFactory;
     import android.graphics.BitmapFactory.Options;
     import android.graphics.Canvas;
     import android.graphics.Region;
     import android.util.Log;
     import android.util.SparseArray;
     import android.widget.Toast;

      import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;
   import java.util.Random;

   public class FruitProjectileManager02 implements ProjectileManager {

private final Random random2 = new Random();
private final List<Projectile> fruitProjectiles = 
   new     ArrayList<Projectile>();
private final SparseArray<Bitmap> bitmapCache;
private Region clip;
private int maxWidth;
private int maxHeight;





private String FruitTypen="FruitType2";


public FruitProjectileManager02(Resources r) {

bitmapCache = new SparseArray<Bitmap>(FruitType2.values().length);

for (FruitType2 t : FruitType2.values()) {
    bitmapCache.put(t.getResourceId(), BitmapFactory.decodeResource(r, t.getResourceId(), new Options()));
}
}

public void draw(Canvas canvas) {
for (Projectile f : fruitProjectiles) {
    f.draw(canvas);
}
}

public void update() {

if (maxWidth < 0 || maxHeight < 0) {
    return;
}

if (random2.nextInt(1000) < 30) {
    fruitProjectiles.add(createNewFruitProjectile());
}

for (Iterator<Projectile> iter = fruitProjectiles.iterator(); iter.hasNext();) {

    Projectile f = iter.next();
    f.move();

    if (f.hasMovedOffScreen()) {
    iter.remove();
    }
}
}

private FruitProjectile02 createNewFruitProjectile() {
int angle = random2.nextInt(20) + 70;
int speed = random2.nextInt(30) + 120;
boolean rightToLeft = random2.nextBoolean();

float gravity = random2.nextInt(6) + 8.0f;
float rotationStartingAngle = random2.nextInt(360);
float rotationIncrement = random2.nextInt(100) / 3.0f;

if (random2.nextInt(1) % 2 == 0) {
    rotationIncrement *= -1;
}

return new FruitProjectile02(bitmapCache.get(FruitType2.randomFruit().getResourceId()), maxWidth, maxHeight,
    angle, speed, gravity, rightToLeft, rotationIncrement, rotationStartingAngle);
}

public void setWidthAndHeight(int width, int height) {
this.maxWidth = width;
this.maxHeight = height;
this.clip = new Region(0, 0, width, height);
}

@Override
public int testForCollisions(List<TimedPath> allPaths) {

int score = 0;
for (TimedPath p : allPaths) {


    for (Projectile f : fruitProjectiles) {

    if(!f.isAlive())
        continue;

    Region projectile = new Region(f.getLocation());
    Region path = new Region();
    path.setPath(p, clip);

    if (!projectile.quickReject(path) && projectile.op(path, Region.Op.INTERSECT))

    {
        if (FruitType2.currentrandom()<=9)
        {score++;}
        else
        {score=score-5;}

        f.kill();




    }
    }









}
return score;
}

}

Complete code for FruitType: package fruitninja;

  import com.example.triviality.R;

  import java.util.Random;


    public enum FruitType2{





 T02(R.drawable.n002)
  , T04(R.drawable.n004), 
    T06(R.drawable.n006),
     T08(R.drawable.n008),
      T10(R.drawable.n010), 
   T12(R.drawable.n012)     
 , T14(R.drawable.n014), 
 T16(R.drawable.n016),
 T18(R.drawable.n018),
 T20(R.drawable.n020), 


OTHER1(R.drawable.n003),
OTHER2(R.drawable.n007),
OTHER3(R.drawable.n011);




;






private final int resourceId;

private FruitType2(int resourceId) {
this.resourceId = resourceId;
}

public int getResourceId() {
return resourceId;
}

private static final Random random = new Random();



public static int currentrandom() {






    return random.nextInt(FruitType2.values().length );
}



public static FruitType2 randomFruit() {






return FruitType2.values()[random.nextInt(FruitType2.values().length )];
}


}

I understand the problem , the current random(when the fruit is generated) is not the same that the random when the fruit is sliced and my question is how to solve this problem. I get no idea so if you have some clues, I am interested.

Thank you in advance.




Aucun commentaire:

Enregistrer un commentaire