## I have a bitmap problem!

Put your problem here if it does not fit any of the other categories.

### I have a bitmap problem!

Why doesn't the bitmap moves?

public void onDraw(Canvas canvas){
SetAng();
setPos();

bullets = new ArrayList<Shot2>();

Shot2 oi = new Shot2(angulo, angulo2, posyE, posyE);

for(int c = 0; c < bullets.size(); c++){
s2 = (Shot2) bullets.get(c);
s2.onDraw(canvas);
}

Matrix mtx2 = new Matrix();
mtx2.reset();

/*Random rand3 = new Random();
shotif = rand3.nextInt(2);*/

canvas.setMatrix(mtx2);
canvas.setMatrix(null);

}

return nv2w;
}

return nv2h;
}

}

public class Shot2{

Enimigos enm;
float rw2, rh2, ox2, oy2, xshot2, yshot2;

public Shot2(float angrot, float ang, float incx, float incy){
if(angshot2 == 0 && xshot2 == 0 && yshot2 == 0 && vxS2 == 0 && vyS2 == 0){
angshot2 = (float) Math.toDegrees(ang);
angshotrot2 = angrot;
ox2 = incx;
oy2 = incy;
}
}

public void setPosShot(){

vxS2 = (float) (Math.cos(angshot2))*6;
vyS2 = (float) (Math.sin(angshot2))*6;

if(xshot2 == 0 && yshot2 == 0 && vxS2 == 0 && vyS2 == 0){
xshot2 = ox2 + (enm.getbadNaveW()/2) - (getRectW()/2);
yshot2 = oy2 + (enm.getbadNaveH()/2) - (getRectH()/2);
}else{
xshot2 += vxS2;
yshot2 += vyS2;
}

}

public void onDraw(Canvas canvas){
setPosShot();

Matrix mtx3 = new Matrix();
mtx3.reset();

rect2 = BitmapFactory.decodeResource(getResources(),R.drawable.rect2);
rw2 = rect2.getWidth();
rh2 = rect2.getHeight();

mtx3.setRotate(angshotrot2, xshot2 + (rw2/2), yshot2 + (rh2/2));

canvas.setMatrix(mtx3);
canvas.drawBitmap(rect2, xshot2, yshot2, null);
canvas.setMatrix(null);
}

public float getRectW(){
return rw2;
}

public float getRectH(){
return rh2;
}

}
MakyOnCover
Junior Developer

Posts: 19
Joined: Sat Dec 31, 2011 7:49 pm

### Re: I have a bitmap problem!

It is very hard to read that code without the indentations.
repost it using Code blocks:
Code: Select all
`[code]all your code here[/code]`

actually put each class in a separate set of code blocks.

But in the mean time. Think about what variables define your bitmap's position and when they get updated and calculated.

BUKsAPPs
http://buksweb.com/
BUKsAPPs
Claustrum Lite - Area capture game
R:P:S Academy - play Rock/Paper/Scissors online
BUKsAPPs
Experienced Developer

Posts: 71
Joined: Sat Jan 16, 2010 9:01 pm
Location: USA

### Re: I have a bitmap problem!

I actually just asked a question on how to do this. It may help out a bit. http://www.anddev.org/post112465.html#p112465
nin2246
Developer

Posts: 42
Joined: Wed Jan 18, 2012 8:08 pm

### Re: I have a bitmap problem!

The main problem is: the bitmap was moving when the variables xshot2 and yshot2 were global but now I putted inside the class Shot2, cause I want to create multiple bitmaps from this class and i can't.
Whats the solution for this problem?
MakyOnCover
Junior Developer

Posts: 19
Joined: Sat Dec 31, 2011 7:49 pm

### Re: I have a bitmap problem!

Is 'rect2' the bitmap that you want to be moving?

You are creating a new shot every call to onDraw(), that seems like a bit of an issue. How often do you call onDraw()?

What does this evaluate to every time:
Code: Select all
`if(xshot2 == 0 && yshot2 == 0 && vxS2 == 0 && vyS2 == 0){   xshot2 = ox2 + (enm.getbadNaveW()/2) - (getRectW()/2);   yshot2 = oy2 + (enm.getbadNaveH()/2) - (getRectH()/2);}else{   xshot2 += vxS2;   yshot2 += vyS2;}`
If the condition is always (true) you will always set the value to the same values and never move.

print out your values into LogCat to see what xshot2 and yshot2 are each time onDraw is called. This will let you know if your update is wrong or your drawing is wrong.

BUKsAPPs
Claustrum Lite - Area capture game
R:P:S Academy - play Rock/Paper/Scissors online
BUKsAPPs
Experienced Developer

Posts: 71
Joined: Sat Jan 16, 2010 9:01 pm
Location: USA

### Re: I have a bitmap problem!

Yes, is the bitmap rect2 that I want to see moving, and I call the onDraw every single moment cause I don't know how can I do the call of the method every 5 seconds.
And that coding I forgot to take out the first if() statment.
Sorry my coding, I learned programing java alone.
MakyOnCover
Junior Developer

Posts: 19
Joined: Sat Dec 31, 2011 7:49 pm

### Re: I have a bitmap problem!

Hi MakyOnCover,

It is a little hard to follow your code. I tried copying it out of there and making it work but its not all there.

I have another example for you. This one moves images around the way I think I was suggesting some time back. It is just a simple bouncing balls animation that keeps balls in an array and then shows them in the onDraw which pretty much repeats as fast as it can. It doesn't do any rotation but it could. It hasn't been optimized or anything to keep it from dying if the app is paused or anything. Just an example of the Ball class, the BallsView class and the activity which hosts them.

Hope this helps.

Phyll

Edit

Replaced the single bitmap used for all 4 moving objects with 4 individual bitmaps. This should eliminate confusion about how many bitmaps are being moved at the same time.
Attachments
MocProj.zip
4 Bitmap Example
Last edited by Phyll on Sat Jan 28, 2012 4:59 am, edited 1 time in total.
Phyll
Master Developer

Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

### Re: I have a bitmap problem!

I saw the ball exemple, but I don't know how to make it with bitmaps. I already made one bitmap move but, with this code I just can create a bitmap, and I was expecting alot of them, why doesn't happen?
MakyOnCover
Junior Developer

Posts: 19
Joined: Sat Dec 31, 2011 7:49 pm

### Re: I have a bitmap problem!

Hi MakyOnCover,

There seems to be some confusion here. The balls ARE bitmaps. 4 separate bitmaps. I just used the same picture for all of them. Didn't think it was necessarry to illustrate the example. I was wrong. So I edited the previous post and replaced the example with one that has 4 different pictures for the bitmaps. Try it.

Hope this helps.

Phyll
Phyll
Master Developer

Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

### Re: I have a bitmap problem!

But how can I do it with an infinite number of times?
MakyOnCover
Junior Developer

Posts: 19
Joined: Sat Dec 31, 2011 7:49 pm

### Re: I have a bitmap problem!

Is your problem making multiple bitmaps or having them move?

Either way, you will never make a infinite number of bitmaps, that is too many. Try to make 2 first, not 1 every call to onDraw.

BUKsAPPs
http://buksweb.com/
BUKsAPPs
Claustrum Lite - Area capture game
R:P:S Academy - play Rock/Paper/Scissors online
BUKsAPPs
Experienced Developer

Posts: 71
Joined: Sat Jan 16, 2010 9:01 pm
Location: USA

### Re: I have a bitmap problem!

OK!
But How can I time my object creation?
MakyOnCover
Junior Developer

Posts: 19
Joined: Sat Dec 31, 2011 7:49 pm

### Re: I have a bitmap problem!

Hi MakyOnCover,

I worked on the example again. Not a great example but it shows how to fire a stream of bullets at the bouncing objects from the previous example. Get this example working and it will fire whenever you touch the screen. The touch x location determines where across the bottom of the screen the bullets will start from. Its really pretty basic but it is a basis for shooting bullets. Give it a try.

Phyll
Attachments
MocProj.zip
Bullets Example
Phyll
Master Developer

Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

### Re: I have a bitmap problem!

Hi MakyOnCover,

So I worked on the example some more. Now you can set the direction of fire by where you touch on the display. It aligns the bullets with their trajectory and keeps score of how many times you hit earth. Anyway, couple of more things that it does.

Hope this helps.

Phyll
Attachments
MocProj.zip
Keep Score Example
Phyll
Master Developer

Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

### Re: I have a bitmap problem!

Ok, I think I get it, I was mistaking with the global variables. Now I get it.
Thank you guys.
MakyOnCover
Junior Developer

Posts: 19
Joined: Sat Dec 31, 2011 7:49 pm