I have a bitmap problem!

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

I have a bitmap problem!

Postby MakyOnCover » Fri Jan 27, 2012 7:37 pm

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);

bullets.add(oi);

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);*/

badNave = BitmapFactory.decodeResource(getResources(),R.drawable.nave2);
nv2w = badNave.getWidth();
nv2h = badNave.getHeight();

mtx2.setRotate(angulo, posxE + (badNave.getWidth()/2), posyE + (badNave.getHeight()/2));

canvas.setMatrix(mtx2);
canvas.drawBitmap(badNave, posxE, posyE, null);
canvas.setMatrix(null);

}

public float getbadNaveW(){
return nv2w;
}

public float getbadNaveH(){
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
Junior Developer
 
Posts: 19
Joined: Sat Dec 31, 2011 7:49 pm

Top

Re: I have a bitmap problem!

Postby BUKsAPPs » Fri Jan 27, 2012 8:22 pm

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
Experienced Developer
 
Posts: 71
Joined: Sat Jan 16, 2010 9:01 pm
Location: USA

Re: I have a bitmap problem!

Postby nin2246 » Fri Jan 27, 2012 8:35 pm

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
Developer
 
Posts: 42
Joined: Wed Jan 18, 2012 8:08 pm

Re: I have a bitmap problem!

Postby MakyOnCover » Fri Jan 27, 2012 9:01 pm

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
Junior Developer
 
Posts: 19
Joined: Sat Dec 31, 2011 7:49 pm

Re: I have a bitmap problem!

Postby BUKsAPPs » Fri Jan 27, 2012 10:25 pm

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.

Your code is so hard to read!!
Help us help you
BUKsAPPs
Claustrum Lite - Area capture game
R:P:S Academy - play Rock/Paper/Scissors online
BUKsAPPs
Experienced Developer
Experienced Developer
 
Posts: 71
Joined: Sat Jan 16, 2010 9:01 pm
Location: USA

Re: I have a bitmap problem!

Postby MakyOnCover » Fri Jan 27, 2012 11:51 pm

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
Junior Developer
 
Posts: 19
Joined: Sat Dec 31, 2011 7:49 pm

Top

Re: I have a bitmap problem!

Postby Phyll » Sat Jan 28, 2012 12:16 am

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
(76.73 KiB) Downloaded 23 times
Last edited by Phyll on Sat Jan 28, 2012 4:59 am, edited 1 time in total.
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Re: I have a bitmap problem!

Postby MakyOnCover » Sat Jan 28, 2012 12:56 am

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
Junior Developer
 
Posts: 19
Joined: Sat Dec 31, 2011 7:49 pm

Re: I have a bitmap problem!

Postby Phyll » Sat Jan 28, 2012 5:06 am

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
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Re: I have a bitmap problem!

Postby MakyOnCover » Sat Jan 28, 2012 2:28 pm

But how can I do it with an infinite number of times?
MakyOnCover
Junior Developer
Junior Developer
 
Posts: 19
Joined: Sat Dec 31, 2011 7:49 pm

Re: I have a bitmap problem!

Postby BUKsAPPs » Sat Jan 28, 2012 4:06 pm

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
Experienced Developer
 
Posts: 71
Joined: Sat Jan 16, 2010 9:01 pm
Location: USA

Re: I have a bitmap problem!

Postby MakyOnCover » Sat Jan 28, 2012 8:04 pm

OK!
But How can I time my object creation?
MakyOnCover
Junior Developer
Junior Developer
 
Posts: 19
Joined: Sat Dec 31, 2011 7:49 pm

Re: I have a bitmap problem!

Postby Phyll » Sat Jan 28, 2012 9:05 pm

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.

Hope this answers your question.

Phyll
Attachments
MocProj.zip
Bullets Example
(84.32 KiB) Downloaded 26 times
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Re: I have a bitmap problem!

Postby Phyll » Sun Jan 29, 2012 6:15 am

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
(88.25 KiB) Downloaded 21 times
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Re: I have a bitmap problem!

Postby MakyOnCover » Sun Jan 29, 2012 7:20 pm

Ok, I think I get it, I was mistaking with the global variables. Now I get it.
Thank you guys.
MakyOnCover
Junior Developer
Junior Developer
 
Posts: 19
Joined: Sat Dec 31, 2011 7:49 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 15 guests