dimanche 29 mars 2015

How to make a variable button that you can click



I'm trying to make a game app that lets you click the green buttons and get rewarded or what ever and displays it and when you click a red button you loose. I've attempted to make a button for this app but cannot wrap my head around it. thank you everyone



public class Main extends Activity {
DrawingView v;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
v = new DrawingView(this);
setContentView(v);

Button redCircle = (Button) findViewById(Color.RED);

redCircle.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent s = new Intent(Main.this, ifRed());
startActivity(s);
}

private Class<?> ifRed() {
// TODO Auto-generated method stub
String str = "You clicked on a red circle. You Loose!";
System.out.print(str);
return null;
}
});
}
}


and here is that class that randomly generates the circles



public class DrawingView extends View {
public DrawingView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}

private int lastColor = Color.BLACK;
private final Random random = new Random();
private final Paint paint = new Paint();
private final int radius = 230;
private final Handler handler = new Handler();

private final Runnable updateCircle = new Runnable() {
@Override
public void run() {
lastColor = random.nextInt(2) == 1 ? Color.RED : Color.GREEN;
paint.setColor(lastColor);
invalidate();
handler.postDelayed(this, 380);
}
};



@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
handler.post(updateCircle);
}

@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
handler.removeCallbacks(updateCircle);
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// your other stuff here
canvas.drawCircle(random.nextInt(canvas.getWidth()-radius/2) + radius/2f, random.nextInt(canvas.getHeight()-radius/2) + radius/2f, radius, paint);
}


}



Aucun commentaire:

Enregistrer un commentaire