I am making an android game which needs to display some different images.
I need to make a method that keeps on generating images from a resource in the drawable directory and then drop the images from the top of the screen to the bottom. I also need that each image should have an id "image-1" "image-2" in which the index number goes higher once more images are created as an integer. I also need them to be generated at different locations at the top of the screen and drop with a specified speed. Once they hit the bottom, I want a method to be executed like:
public void touchedGround() {
//My code
}
I am using this code so far:
import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;
public class GameScreenActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gamescreen);
generateParachuters(savedInstanceState);
}
private void generateParachuters(Bundle icicle) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "generating images", 10).show();
}
}
I know one way to do it, but it requires a lot more code then what you're probably looking for. But hey, here goes.
Create a class for your falling object, which I'm guessing is a Parachuter. This class will have an X and Y variable to keep track of where it is on the screen. You can then have a tick method, which will increase the Y variable by however fast you want it to fall. You can also have them keep track of a integer ID if you want.
Create a list that will hold the Parachuter objects. I use an ArrayList. This way you create a Parachuter when needed and add it to the list. Cycle through the list to draw each one.
You need something to draw the Parachuters with. Personally, I use Canvas from SurfaceView.
You'll need a looping animation. Loop through, each time drawing the Parachuters and calling tick on each one.
During the loop, you can check the position of each Parachuter. If its position goes beyond Canvas.getHeight(), remove the image from the list.
Well, that's quite a bit there. Ask me if you need further clarification.
UPDATE: Here's a fully-working example.
UPDATE 2:
//Check right edge of screen
for (int i = 0; i < parachuters.size(); i++)
{
if (parachuters.get(i).getX() > canvas.getWidth())
//Do whatever it is you want
}
Related
So I'm trying to make a little game for Android. It's about turning lights on and off. I have 25 ImageButtons and, when I press one, I need the ones that are adjacent (up, left, down, right) to change too.
The thing is that I can't find a way to get/identify this buttons.
I tried using "view.getX()" and "view.getY()" but don't know how to implement them the right way (if there is one).
Here's the code I use to change ONE button:
public void change(View view)
{
String state = view.getTag().toString();
if (state.equals("on"))
{
view.setBackgroundDrawable(getResources().getDrawable(R.drawable.lightOff));
view.setTag("off");
lightsOn -= 1;
txtNumLights.setText(String.valueOf(lightsOn));
}
if (state.equals("off")) {
view.setBackgroundDrawable(getResources().getDrawable(R.drawable.lightOn));
view.setTag("on");
lightsOn += 1;
txtNumLights.setText(String.valueOf(lightsOn));
}
}
I use the tags to know if it's On or Off and set a txtView with the number of ON lights.
But, to the point, how do I get the ones adjacent to this one?
Thanks in advance.
You have to store the buttons on a bidimensional array.
When you click on one button, you set the tag and the background on the buttons you are interested on.
Pseudocode:
If (button_matrix[row][column] is clicked) then
Set_tag_background (button_matrix[row-1][column])
Set_tag_background (button_matrix[row+1][column])
Set_tag_background (button_matrix[row][column-1])
Set_tag_background (button_matrix[row][column+1])
Set_tag_background (button_matrix[row][column])
...
On the implementation you should take care about not generating ArrayOutOfBoundsException...
Based on this answer, I created a arrayList of rectF.
Technique to make a canvas drawLine() clickable?
here's the logic of my code :
List<RectF> rectFs;
Point pt1;
Point pt2;
then
path.moveTo(pt1.x, pt1.y);
path.lineTo(pt2.x, pt2.y);
path.computeBounds(rectF, true);
rectFs.add(rectF);
and then, I have this method to check the clicked and the rectF arrayList.
void lineHighighted(Point pt) {
int ct = 0;
for(RectF rectF : rectFs) {
if(rectF.contains(pt.x, pt.y)) {
ct++;
Log.d(tag, ct + "HERE");
}
}
}
my problem is, sometimes, the whole arraylist is selected or "called" even I didn't touch that "line".
Any wrong in my code?
Thanks in advance.
ADDITIONAL :
I found out that after adding this code in my canvas :
path.moveTo(coor1[0], coor1[1]);
path.lineTo(coor2[0], coor2[1]);
canvas.drawPath(path, paint2);
path.computeBounds(rectf, true);
my previous result :
it becomes like this :
It might be!Because i don't see your code of interaction with canvas ill post the code which fully working.The logic is that one line mustn't overlay another one in case to proper function.hope i could help you.
// setting where I will draw the ImageView for taking pictures
// rec is used for onInterceptTouchEvent. I pass this from the
// highest to lowest layer so that when this area of the screen
// is pressed, it ignores the TouchView events and passes it to
// this activity so that the button can be pressed.
rec.set((int)((double)mScreenWidth*.85),
(int)((double)mScreenHeight*.10) ,
(int)((double)mScreenWidth*.85)+mButtonDrawable.getMinimumWidth(),
(int)((double)mScreenHeight*.70)+mButtonDrawable.getMinimumHeight());
mButtonDrawable = null;
By This Logic you can use whatever you want in this logic it lets click happen not being overlaid
This question refers to andengine GL-ES1.
I am having trouble making a wallpaper activity that refreshes when your return to it, or when the user performs actions in the game.
My game has two activities. In one, you can edit and arrange the background elements in a room. In the other you play the game, and it uses the same background with the elements you arranged in the first activity.
There is also a live wallpaper in which your room is the background and characters move around in front of it.
I am making updtaes in onResume() in the wallpaper.
first I detach all the backgroudn sprites i used before.
Then I attach new sprites in the updated positions.
What happens is: some of the sprites don't show up.
Here is the method: Can you see anything I'm doing wrong?
private void loadBackgroundDecorations() {
//Add new decorations
Log.d(TAG, "loadBackgoundDecorations");
mEngine.runOnUpdateThread(new Runnable() {
#Override
public void run() {
// Remove Old Decorations
Log.d(TAG, "loadBackgoundDecorations: decorationList.size() =" + decorationList.size());
while(decorationList.size() > 0){
Sprite d = decorationList.remove(0);
scene.detachChild(d);
Log.d(TAG, "loadBackgoundDecorations: detachChild");
}
decorationList.clear();
//Add new decorations
ArrayList<Integer> decorations = app.getBackgroundManager().getDecorations();
Log.d(TAG, "loadBackgoundDecorations: decorations.size()" +decorations.size());
for (int i = 0; i < decorations.size(); i+=3) {
Log.d(TAG, "Decoration Values: texture-"+decorations.get(i)+", x-"+decorations.get(1+i)+", y-"+decorations.get(2+i));
Sprite draggable = new Sprite(decorations.get(1+i),decorations.get(2+i),mGameTextureRegionLibrary.get(decorations.get(i)));
draggable.setIgnoreUpdate(true);
scene.attachChild(draggable,0);
decorationList.add(draggable);
Log.d(TAG, "loadBackgoundDecorations: attachChild"+ i);
}
}
});
}
#Override protected void onResume() {
super.onResume();
Log.d(TAG, "onResume");
BitmapTextureAtlasTextureRegionFactory.createFromAsset(mTexture, this, app.getAquariumBackground(), 0, 0);
addBubbles();
loadBackgroundDecorations();
addCharacters()
}
============ UPDATE =================
As some people have suggested below, I tried adding all the scene setup functions into the runnable. This has no effect. What has worked for me is to set the "wrong" decorations visible property to "false". But I am worried that this will eventually cause a memory leak as more and more duplicates of the sprites are hidden on the wallpaper.
The problem only exists when I call "detachChild". For some reason that seems to prevent "attachChild" from firing correcly. Anybody have ideas for what could be causing this?
Can anyone else create an activity that adds and removes sprites in the onResume function?
I am fairly certain that the error has to do with your onResume method. The order you have your methods in is
addBubbles();
loadBackgroundDecorations();
addCharacters()
but your loadBackgroundDecorations uses a runnable so there is no guarantee that the method will run in between.
My Explanation:
From what I understand both addCharacter and addBubbles will be running on the UIthread whereas the loadBackgroundDecorations method will run on the update thread. The two threads will go through the methods at different times and that is where you are seeing some inconsistencies.
To Fix:
Put addBubbles and addCharacters in the same runnable in the order that you want and it should work as expected.
I am trying to make a onTouchEvent to create a missile that will launch from my character sprite and forward.
I have this working using
if (missdraw = true){
canvas.drawBitmap(missile,missilex,missileY,null);
missilex = missilex + 14;
missdraw = false;
}
in my onDraw method, but the problem is it will only create one at a time.
I tried to create a class to deal with this, but this just causes an error and crashes when i try to fire.
here is what i use for the class: (this is in the ondraw in my gameview)
for (Batcher missile : missiles ){
missile.onDraw(canvas);
}
this is in the class
public Batcher(List<Batcher> temps, ScreenActivity newView, float x,
float y, Bitmap missile){
this.x = 1;
this.y = 2;
this.missile = missile;
}
public void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
canvas.drawBitmap(missile, x,y, null);
}
I would appreciate any help, but also if you could explain how it would work, instead of just code, as im quite new to programming, and really need to understand what im doing and why im doing it, rather than just copying peoples code because it works.
Cheers Phil.
Your concepts are pretty good, actually. There are a few things I don't quite understand about the code samples you posted up, I'll try to translate into what I'd do and you can tell me if I'm doing it wrong :)
in your game class you need (and it looks like you have) a list of Missiles:
LinkedList<Batcher> missiles;
In your onTouch(), however a missile is created -
missiles.add(new Batcher(missilex, missiley, missile));
you now have a collection of missiles. Note that I didn't include the list in the constructor of your batcher, because an object should never need to know that it's a part of a collection. All it needs to know is how to draw itself and where. Since I assume that all of your missiles will be added to or removed from the screen frequently, while only having a few on screen at a time, I've used a LinkedList, which is fast for adding and removing, but slow for accessing a specific missile. If you needed to access specific items in the collection and the collection didn't change very much, you would use an ArrayList instead. on to onDraw - as you have it the missile draws itself, which is fine, but I prefer to let the View do the drawing, with the missile telling it where it should be drawn -
for (Batcher missile : missiles ){
missile.setX(missile.getX() + 14); // to make it move
if (missile.getX() > canvas.gedWidth()) { //check if it's left the screen
missiles.remove(missile); // Remove it
}
else { //perform drawing
canvas.drawBitmap(missile.getBitmap(), missile.getX(), missile.getY(), null);
}
}
Hopefully that'll do it for you, but feel free to let me know if there's anything you'd like me to explain more!
I'm trying to get it so that when the user clicks popbtnnext within the popup, the program checks if the rest of the array flaggedwordsused is null. If not, the button is set to be visible and the program moves on. If it is null, the program updates the button to be invisible.
popbutnext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
f++;
for (int g= f+1; g<flagwordsused.length; g++) {
if (flagwordsused[g] != null) {
popbutnext.setVisibility(0);
break;
}
else{
poptv1.setText(""+f);
popbutnext.setVisibility(1);
}
}
}
});
flagwordsused is an array of 20 elements, f in my test case starts at 0 with the string "very" occupying element zero and all other values are null. I have a textView updating with values of f so that I know the program is getting to the else statement. That value is updating just fine but the visibility of the button never changes. Any help you could give me would be appreciated. The popuplayout was created in java, not xml if that makes any difference.
You should use the constants in View to set visibility instead. Makes the code easier to read too:
To make popbutnext invisible:
popbutnext.setVisibility(View.INVISIBLE);
To make popbutnext invisible and not take any space when building the layout:
popbutnext.setVisibility(View.GONE);
To make popbutnext visible:
popbutnext.setVisibility(View.VISIBLE);
The reason that it does not work for you is because the values are wrong. Invisible is 4, gone is 8, visible is 0. However, I still recommend to use the constants in the View class - you never know, they might change in coming Android versions. Take a look at this link, and the visibility parameter.