i got confused with StaticClass code which given from my friend to use GUI handler to alternative save besides Shared Preferences
I had this following code in my selectlevel.class
public class selectlevel extends Activity {
Button f1, f2, f3;
ImageView f2lock, f3lock;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.selectlevel);
f1 =(Button)findViewById(R.id.f1);
f1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
// TODO Auto-generated method stub
Intent level1 = new Intent ();
level1.setClassName ("com.example.game", "com.example.game.levelone");
startActivity (level1);
}
});
}
f2=(Button)findViewById(R.id.f2);
f2lock=(ImageView)findViewById(R.id.f2lock);
f2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
// TODO Auto-generated method stub
Intent level2 = new Intent ();
level2.setClassName ("com.example.game", "com.example.game.leveltwo");
startActivity (level2);
}
});
}
}
so my friend give StaticClass code with GUI Handler to make button VISIBLE when latest level finish();
and save it
this is the following code
static class PlayerProgress {
// We set progress to static, so it would be alive, no matter what activity you're in.
private static int progress = 1;
/**
* Update the player's progress.
* @param levelNumber: latest level number.
*/
public static void updateProgress(int levelNumber) {
progress = levelNumber;
}
/**
* Get the player's progress.
* @return the latest level number.
*/
public static int getPlayerProgress() {
return progress;
}
}
/**
* The gui handler would need to be called, every time you need to update the screen to the
* appropriate level and it's assets. (Buttons, activities ect.)
*
* I would implement a MainActivity, which would handle the different screens.
*
*/
class guiHandler {
public void updateLevel() {
int progress = PlayerProgress.getPlayerProgress();
/*
* Add your
*/
switch(progress) {
case 1:
f2.setVisibility(View.VISIBLE);
f2lock.setVisibility(View.GONE);
break;
case 2:
f3.setVisibility(View.VISIBLE);
f3lock.setVisibility(View.GONE);
break;
// You can expand this to as many levels you'd like.
}
}
}
I'm just confused how to combine my code and the StaticClass code that given from my friend to save the button Visibility when latest level completed
Can anyone help me with the code?
UPDATE
i made a change with my code like this
f1=(Button)findViewById(R.id.f1);
f1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
// TODO Auto-generated method stub
Intent level1 = new Intent ();
level1.setClassName ("com.example.game", "com.example.game.levelone");
startActivity (level1);
}
});
f2=(Button)findViewById(R.id.f2);
f2lock=(ImageView)findViewById(R.id.f2lock);
f2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
// TODO Auto-generated method stub
Intent level2 = new Intent ();
level2.setClassName ("com.example.game", "com.example.game.leveltwo");
startActivity (level2);
}
});
updateLevels();
}
static class PlayerProgress {
private static int progress = 0;
public static void updateProgress(int levelNumber) {
progress = levelNumber;
}
public static int getPlayerProgress() {
return progress;
}
}
public void updateLevels() {
int progress = PlayerProgress.getPlayerProgress();
switch(progress) {
case 1:
f2.setVisibility(View.VISIBLE);
f2lock.setVisibility(View.GONE);
break;
case 2:
break;
// You can expand this to as many levels you'd like.
}
}
and use this in my levelone.class
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
selectlevel.PlayerProgress.updateProgress(1);
finish();
when levelone.class
finish f2 button still GONE, how to fix this?
So, you can use this code
selectlevel.java
package com.example.myapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
public class selectlevel extends Activity {
Button f1, f2, f1lock, f2lock;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.selectlevel);
f1 = (Button) findViewById(R.id.f1);
f1lock = (Button) findViewById(R.id.f1lock);
f1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent level1 = new Intent();
level1.setClassName("com.example.myapp", "com.example.myapp.levelone");
startActivity(level1);
}
});
f2 = (Button) findViewById(R.id.f2);
f2lock = (Button) findViewById(R.id.f2lock);
f2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent level2 = new Intent();
level2.setClassName("com.example.myapp", "com.example.myapp.leveltwo");
startActivity(level2);
}
});
updateLevels();
}
static class PlayerProgress {
// We set progress to static, so it would be alive, no matter what activity you're in.
private static int progress = 0;
/**
* Update the player's progress.
*
* @param levelNumber: latest level number.
*/
public static void updateProgress(int levelNumber) {
progress = levelNumber;
}
/**
* Get the player's progress.
*
* @return the latest level number.
*/
public static int getPlayerProgress() {
return progress;
}
}
public void updateLevels() {
int progress = PlayerProgress.getPlayerProgress();
switch (progress) {
case 0:
f1.setVisibility(View.VISIBLE);
f1lock.setVisibility(View.GONE);
f2.setVisibility(View.GONE);
f2lock.setVisibility(View.VISIBLE);
break;
default:
f1.setVisibility(View.VISIBLE);
f1lock.setVisibility(View.GONE);
f2.setVisibility(View.VISIBLE);
f2lock.setVisibility(View.GONE);
break;
}
}
}
selectlevel.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:layout_width="200dp"
android:layout_height="100dp"
android:id="@+id/f1"
android:text="Level 1"/>
<Button
android:layout_width="200dp"
android:layout_height="100dp"
android:id="@+id/f1lock"
android:text="Level 1 disabled"/>
<Button
android:layout_width="200dp"
android:layout_height="100dp"
android:id="@+id/f2"
android:text="Level 2"/>
<Button
android:layout_width="200dp"
android:layout_height="100dp"
android:id="@+id/f2lock"
android:text="Level 2 disabled"/>
</LinearLayout>
And in your level Activity update progress
levelone.java
package com.example.myapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class levelone extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.levelone);
Button finishf1 = (Button) findViewById(R.id.finishf1);
finishf1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
selectlevel.PlayerProgress.updateProgress(1);
startActivity(new Intent(levelone.this, selectlevel.class));
}
});
}
}
levelone.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:layout_width="200dp"
android:layout_height="100dp"
android:id="@+id/finishf1"
android:text="Finish Level 1"/>
</LinearLayout>