I'm trying to make an android game, i'm not new to programming but new to developing games, i made a 2D map in PHP on server side, it has 5 kind of tiles, the whole map has about 100000 tiles, I know I should show it in small parts, every thing works fine but I don't know how should I show it in android side, i'm grateful for any guide...
what XML layout should I use for this map??
am I wrong about everything above and i need to make a game map by another way?
if there is a better way to make a smooth map like google map for a game, I would be very grateful to let me know about that...
I have tried table layout but I have got some lag on that, I want this to work smoothly like google map
for (int i = 0; i < 11; i++) {
TableRow tableRow1 = new TableRow(getApplicationContext());
tableRow1.setBackgroundColor(getResources().getColor( R.color.colorPrimary));
tv1.addView(tableRow1);
for (int j = 0; j < 11; j++) {
final ImageButton img = new ImageButton(this);
img.setPadding(0,1,0,1);
img.setClickable(true);
if ( TheTile == 1 )
{
img.setImageResource(R.drawable.plain);
}
if ( TheTile == 2 )
{
img.setImageResource(R.drawable.woods);
}
if ( TheTile == 3 )
{
img.setImageResource(R.drawable.rocks);
}
if ( TheTile == 4 )
{
img.setImageResource(R.drawable.city);
}
if ( TheTile == 5 )
{
img.setImageResource(R.drawable.lake);
}
tableRow1.addView(img);
}
}
I want a solution to make it work smoothly and user can zoom in and zoom out, but have some problem about choosing xml layout
Related
this is my first question here so I apologize in advance for any question format mistakes. I am trying to make a sound quiz with the sounds of a game. The idea is for the user to click on the button (round speaker icon) and play the sound. Then, choose one of the four images that correspond to the icon of that sound. App layout On every click on one of the ImageViews all four ImageViews should change the resources. However, when I click on any of those ImageViews the app becomes really slow and I get the dialog asking me to wait for it to respond or click OK to close it.
The CPU monitor shows really high percentage at that moment. memory and CPU usage monitor But, I do not get any Memory Out of Bounds Exception.
This is the code of my Array and ArrayLists
int chosenSound;
int locationOfCorrectAnswer = 0;
Integer[] answers = new Integer[4];
ArrayList<Integer> sounds = new ArrayList<Integer>(Arrays.<Integer>asList(R.raw.dismember, R.raw.duel, R.raw.glimpse, R.raw.reapers_scythe, R.raw.vacuum));
ArrayList<Integer> icons = new ArrayList<Integer>(Arrays.<Integer>asList(R.drawable.dismember, R.drawable.duel, R.drawable.glimpse, R.drawable.reapers_schyte, R.drawable.vacuum));
And this is the method for generating new question:
public void generateQuestion() {
Random random = new Random();
chosenSound = random.nextInt(sounds.size());
if (alreadyUsedSounds.size() == icons.size()) {
Toast.makeText(getApplicationContext(), "You used all the sounds!!!", Toast.LENGTH_SHORT).show();
return;
} else {
while (alreadyUsedSounds.contains(icons.get(chosenSound))) {
chosenSound = random.nextInt(sounds.size());
}
}
locationOfCorrectAnswer = random.nextInt(4);
int incorrectAnswerLocation;
for (int i = 0; i < 4; i++) {
if (i == locationOfCorrectAnswer) {
answers[i] = icons.get(chosenSound);
} else {
incorrectAnswerLocation = random.nextInt(sounds.size());
while (incorrectAnswerLocation == chosenSound || Arrays.asList(answers).contains(icons.get(incorrectAnswerLocation))) {
incorrectAnswerLocation = random.nextInt(sounds.size());
}
answers[i] = icons.get(incorrectAnswerLocation);
}
}
button0.setImageResource(answers[0]);
button1.setImageResource(answers[1]);
button2.setImageResource(answers[2]);
button3.setImageResource(answers[3]);
}
Now, I am not sure how to set those ImageView resources without the app crashing. I guess there is a pretty much straightforward way to do this, but being a beginner I would really appreciate any help. Thank you in advance.
Good evening everyone.
I've been messing a bit with isometric tile worlds and I have a few doubts about rendering the elements on it.
When I build a single height map I render it first and then add the diferent elements on top, and the displacement of the last seem right.
public void render(Graphics2D g2d) {
for(int i = 0; i < tileGrid.length; i++) {
Point isop = NeoMath.getInstance().cartToIso(i % yAmmount, i / yAmmount, GlobalDataStorage.tileWidth, GlobalDataStorage.tileHeight);
TileManager.getInstance().getTileByID(tileGrid[i]).render(g2d, isop.x, isop.y);
}
for(Entity entity : entityList) {
entity.render(g2d);
}
}
(The position of the entity is calculated inside it's update).
With this I have no problems as everything is rendered on the same height, the problem comes when I try to add other floors to it.
Let's say that I want it to have three heights. I have a list of list of tiles instead of the single array, and render every element on them:
public void render(Graphics2D g2d) {
int flag = 0;
for(int i = 0; i < tileGrid.size(); i++) {
Point isop = NeoMath.getInstance().cartToIso(i % yAmmount, i / yAmmount, GlobalDataStorage.tileWidth, GlobalDataStorage.tileHeight);
for(int k = 0; k < tileGrid.get(i).size(); k++) {
TileManager.getInstance().getTileByID(tileGrid.get(i).get(k)).render(g2d, isop.x, isop.y - (GlobalDataStorage.tileZ * k));
}
while(flag < currentList.size() &&
currentList.get(flag).getPosition().equals(new Point(i % yAmmount, i /
yAmmount))) {
currentList.get(flag).render(g2d);
flag++;
}
}
}
Where the currentList is the list of entities.
With this I have the problem that, when the entities move to a new position, they get overlaped by the tiles, as these are rendered after the entity, and the position of the entity does not change until it reached the destiny. I could change the position to the new one before rendering, but that implies that in the other axis the previous tile get rendered after the entity, making it disapear for a second due to the overlap.
This also mess when I try to draw selection rectangle as it get stuck behind the tiles being rendered. I don't want them to overlap the whole map so can't draw them after all the rendering has been done either.
Does someone know of another approach that I can try out?
Thank you beforehand.
Draw your entire floor layer in a first pass. Then in the second pass draw all walls and objects and moving entities.
I could change the position to the new one before rendering,
David Brevik, programmer on Diablo, mentions using this option in his GDC talk Diablo: A Classic Games Postmortem. It was his first "What Went Wrong" example!
Reference: https://www.youtube.com/watch?v=VscdPA6sUkc&t=20m17s
Turns out this is a classic hurdle in isometric games.
I was following a basic tutorial on how to start animating things in a mobile app created with Codename One.
https://www.codenameone.com/manual/animations.html
While I get the concept of using layout animation and unlayout animation on containers to move things into place and out of place, this does not seem to work properly for the unlayout animation.
The thought of below code is to have a list of elements that are either shown or hidden based on some search results (the example has just plain labels).
Animating things in place works well when used alone, as shown in the tutorial.
But why does everything vanish completely when calling "animateUnlayoutAndWait()"?
Form hi = new Form("Layout Animations", new BoxLayout(BoxLayout.Y_AXIS));
Button button = new Button("Hide");
hi.add(button);
for (int iter = 0; iter < 10; iter++) {
Label b = new Label("Label " + iter);
b.setWidth(button.getWidth());
b.setHeight(button.getHeight());
b.setY(-button.getHeight());
hi.add(b);
}
button.addActionListener((e) -> {
// hide every second label on click
if (button.getText().equals("Hide")) {
button.setText("Show");
for (int iter = 1; iter < hi.getContentPane().getComponentCount(); iter += 2) {
Component c = hi.getContentPane().getComponentAt(iter);
c.setHidden(false);
}
hi.getContentPane().animateUnlayoutAndWait(500, 0);
}
// show stuff again
else {
button.setText("Hide");
for (int iter = 1; iter < hi.getContentPane().getComponentCount(); iter += 2) {
Component c = hi.getContentPane().getComponentAt(iter);
c.setHidden(true);
}
hi.getContentPane().animateLayoutAndWait(500);
}
});
hi.show();
Thanks and best regards
I noticed that you mistakenly swapped setHidden() values, the if part should true while the else part should be false. Also, remove the AndWait from your animation.
Form hi = new Form("Layout Animations", new BoxLayout(BoxLayout.Y_AXIS));
Button button = new Button("Hide");
hi.add(button);
for (int iter = 0; iter < 10; iter++) {
Label b = new Label("Label " + iter);
b.setWidth(button.getWidth());
b.setHeight(button.getHeight());
b.setY(-button.getHeight());
hi.add(b);
}
button.addActionListener((e) -> {
// hide every second label on click
if (button.getText().equals("Hide")) {
button.setText("Show");
for (int iter = 1; iter < hi.getContentPane().getComponentCount(); iter += 2) {
Component c = hi.getContentPane().getComponentAt(iter);
c.setHidden(true); //should be true here
}
hi.getContentPane().animateUnlayout(500, 255, null); //remove AndWait
} // show stuff again
else {
button.setText("Hide");
for (int iter = 1; iter < hi.getContentPane().getComponentCount(); iter += 2) {
Component c = hi.getContentPane().getComponentAt(iter);
c.setHidden(false); //should be false here
}
hi.getContentPane().animateLayout(500); //remove AndWait
}
});
hi.show();
Couple of notes:
- true and false is correct. haven't paid attention to it, as the result was that everything vanished in both cases anyways.
- whether I wait for it to finish or not does not matter in this case, as I call it only case and not repeatedly on several elements.
- one more thing you changed is setting the target opacity of the unlayout animation from 0 to 255. This way, all elements remain visible.
This brings me to two questions:
1. why is unlayout animation applied to all elements in my form, not only to those whose properties were changed (position values)? When using
animateUnlayoutAndWait(500, 0);
everything will vanish, even the button.
with the current solution from diamond, when calling unlayout animation, the individual elements that should disappear vanish instantly, the only thing element being animation is label 8, and it is animation INTO place, rather than out of place.
I'm making a sliding puzzle ( 3x3/4x4/5x5 with the lower-right corner cut out ). However I can't figure out where to start with programmatically cutting images ( which will be loaded from own gallery in sdcard or database from app ) in the puzzle pieces.
I've been looking over the internet and nothing really helped me.
What is the best way to cut up this image and store it in a new database (and still will be able to slide them)? Just a push in the right direction would be appreciated.
Check the PhotoGaffe app..
Its available on Google code here.
It allows user to choose between 3x3, 4x4, 5x5, and 6x6 puzzles.
This may help you in doing your task.
Straight from something I'm working on at the moment!
Bitmap main = BitmapFactory.decodeResource(getResources(), R.drawable.puzzle);
if( main.getHeight() > main.getWidth() ){
rescalefactor =((float)screenHeight)/main.getHeight();}
else {
rescalefactor = ( (float)screenWidth)/main.getWidth();
}
main = Bitmap.createScaledBitmap(main,(int)(main.getWidth()*rescalefactor),(int)(main.getHeight()*rescalefactor), false);
Bitmap cropped;
LinearLayout layout[] = new LinearLayout[rows];
int x=0,y=0,i=0,j=0,width=main.getWidth()/column,height=main.getHeight()/rows;
int count = 1;
for(i=0;i<rows;++i)
{
layout[i] = new LinearLayout(this);
for(j=0;j<column;++j)
{
cropped = Bitmap.createBitmap(main,x,y,width,height);
image[i][j] = new Tile(this);
image[i][j].setImageBitmap(cropped);
image[i][j].row =i;image[i][j].column =j;
image[i][j].setPadding(1, 1, 1, 1);
image[i][j].setOnClickListener(this);
image[i][j].setDrawingCacheEnabled(true);
image[i][j].setId(count); count++;
layout[i].addView(image[i][j]);
x += width;
}
x = 0; y += height;
root.addView(layout[i]);
}
This is the line where the work is really done:
cropped = Bitmap.createBitmap(main,x,y,width,height);
The Tile class is super simple. Just an extended ImageView with row and column fields:
public class Tile extends ImageView {
public int row, column;
public Tile(Context context)
{ super(context);}
}
I am developing an Android game in java where I will have a sprite that follows the user's finger and is supposed to fire a bullet every second. In other words, I am trying to attach a bitmap that moves up every second. The bitmap starts at the x and y coordinates of the main character sprite. I can't get it to draw more than one missile at a time, and I have run out of ideas of how to do so. I have tried so many things, and I really could use some help.
By the way, my Main Game Panel class extends a surfaceView and implements a SurfaceHolder.Callback:
public class MainGamePanel extends SurfaceView implements SurfaceHolder.Callback{
Thanks!
As far as I understand you want to have the ability to shoot more than 1 bullet at a time? You can use a Vector or Array to do this. With an Array you can set a default amount of visible bullets and in a Vector you could have as mant bullets that your finger is capable of producing.
Here's my code that I use to generate lasers (I store the values in an Array).
public void updatePlayerLaser(boolean shootLaser) {
// Check if a new Laser should be created
if(shootLaser == true) {
if(timeLastCreatedLaser + 100 < System.currentTimeMillis()) {
timeLastCreatedLaser = System.currentTimeMillis();
boolean createdNewLaser = false;
for(int i = 0; i < this.amountOfVisibleLasers; i++) {
if(createdNewLaser == false) {
if(holderLaser[i].isDisposed()) {
this.generateNewLaser(i);
createdNewLaser = true;
}
}
}
}
}
// Update all the other Lasers
for(int i = 0; i < this.amountOfVisibleLasers; i++) {
if(holderLaser[i].isDisposed() == false) {
holderLaser[i].update();
}
}
}
Disposed in this contexts means that the laser is dead, thus making space for a new laser to take the old lasers spot.