Reputation: 37721
I want to simulate a rocket on a screen. When the user press a key, the rocket is activated and the object must go up, and when the key is not pressed, the rocked is deactivated and must go down because of the gravity.
Also will be nice to move the object to the left or the right if the user tilts the phone.
How can this be achieved? There are sample examples of how to do this?
I am not using any engine, I'm doing it all with ObjectAnimator and simple views. It is a very simple game in which I don't want to use a external engine if it is not mandatory.
Upvotes: -2
Views: 754
Reputation: 4182
Ok so, if I understood you correctly, and since you already have a game loop working on updating render/logic of your game for every cycle, I can conclude you have something similar to this:
//Game loop:
while(running){
//game cycle starts here:
//---------
//code handling fps
//---------
update();
render();
//---------
//code handling fps
//---------
}
The 2 main steps being executed every game loop (normally) are updating game logic, and updating screen display depending on that logic (rendering).
Both these method's are part of the game engine, and whenever you are adding new entities into the screen, they need to "follow these rules" (the design pattern of your engine).
Here is a basic example following up the previous code:
private class GameEngine extends Activity {
private boolean running;
@Override
protected void onCreate(Bundle savedInstanceState){
//We initiate game loop:
running = true;
new Thread(new GameLoop ()).start();
}
private void update(){
}
private void render(){
}
class GameLoop implements Runnable {
@Override
public void run() {
while(running){
//game cycle starts here:
//---------
//code handling fps
//---------
update();
render();
//---------
//code handling fps
//---------
}
}
}
}
Now as I can understand from your post you're overriding android api View class for the Rocket (and probably any other entity on screen i bet). Knowing that, what you need to do is, first off, generate logic for position/velocity based on user input (pressing a button/not pressing a button) for every update() call on the engine. After that and with the resulting values (of every particular Entity) updateSelf() call (such as: x,y,velocity,previousX,previousY....), the engine will issue a call to renderSelf(), which in your case it could be translated into a onDraw() callback of the entity, where you would update the view position on given values.
So we need a reference of the object (in this case a rocket) for the engine to loop to both update() and render(). We can store a reference of every entity we want to display on an ArrayList for the engine to work with. These "entities" will all have an implementation of updateSelf() and renderSelf() for the respective steps of the engine cycle. Main class now should look something like this:
private class GameEngine extends Activity {
private boolean running;
private ArrayList<Entity> mEntitiesOnScreen;
@Override
protected void onCreate(Bundle savedInstanceState){
//We create a Rocket for the game loop:
mEntitiesOnScreen = new ArrayList<Entity>();
mEntitiesOnScreen.add(new Rocket());
//We initiate game loop:
running = true;
new Thread(new GameLoop ()).start();
}
private void update(){
for (int i=0; i<= mEntitiesOnScreen.size(); i++){
mEntitiesOnScreen.updateSelf();
}
}
private void render(){
for (int i=0; i<= mEntitiesOnScreen.size(); i++){
mEntitiesOnScreen.renderSelf();
}
}
class GameLoop implements Runnable {
@Override
public void run() {
while(running){
//game cycle starts here:
//---------
//code handling fps
//---------
update();
render();
//---------
//code handling fps
//---------
}
}
}
}
Having something similar to the above in place, the implementation the rocket class could be something like:
public class Rocket extends Entity {
int currentX, currentY = 0;
int velocityX, velocityY = 0;
/*We override these methods from Entity superclass,
which would be a custom class
extending View and implementing a custom interface
(could be named CustomEntityCycleInterface for example)
for updateSelf() and renderSelf()*/
@Override
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
//Here it will go drawing code, using updated values
//of currentY and currentX
}
@Override
public void updateSelf(){
if (isUpPressed){
velocityY = velocityY + 1;
}else{
velocityY = velocityY - 1;
}
currentY = currentY + velocityY;
}
@Override
public void renderSelf(){
//not sure this call will work synchronized
//on a custom game loop, you will have to check:
this.invalidate();
}
}
I know this is REALLY rough off the top of my head right now, and some parts are missing (such as onDraw logic) but should hopefully give you a general idea on how to approach the issue.
Upvotes: 2