Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

How to destroy and re-create / re-paint an image / object
Started by benlebt Nov 13 2010 01:20 AM

7 replies to this topic
[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

benlebt

[GLOBAL: userInfoPane.html]
benlebt
  • Enthusiast

  • 48 posts
  • Guests

Hi,

I'm trying to build a simple snowball game. Two guys, one snowball. When the snowball hits the opposite guy or the floor, it should disappear and re-appear in the hand of the throwing guy.

Sounds simple. But I'm having problems with re-initializing the snowball after it hit the bottom or the other guy. My code looks like this:

.....
......

local floor = ........

physics.start()
physics.setGravity(0, 0)
physics.setDrawMode( "normal" )

local snowball = display.newImage("snowball.png")
snowball.x = 50
snowball.y = display.stageHeight - 60
physics.addBody( snowball, { density=0.3, friction=0.3, bounce=0.1, radius=20 } )
snowball:addEventListener( "touch", dragBody )	

local function dragBody( event )
	physics.setGravity(0, 10)
	gameUI.dragBody( event, { maxForce=55, frequency=5, dampingRatio=10} )
end

local function onCollision( event )
        if event.object1 == floor then	
        	if ( event.phase == "began" ) then
			local oldsnowball = event.object2			
			oldsnowball:removeSelf()			
			local snowball = display.newImage("snowball.png")
			snowball.x = 50
			snowball.y = display.stageHeight - 60	
			snowball:addEventListener( "touch", dragBody )	
		end
        end
end
The problem is: the new snowball appears but as soon as I try to drag it, the whole Corona Simulator crashes. So I can only drag the first snowball. If I try to re-add the new snowball to the physics with physics.addBody in onCollision(), the game crashes as well.
Do you guys have any hints what I'm doing wrong?
uid: 11219 topic_id: 3616 reply_id: 303616


[TOPIC: post.html]
#2

jmp909

[GLOBAL: userInfoPane.html]
jmp909
  • Corona Geek

  • 1,328 posts
  • Corona SDK

try this.... i haven't tried it but is a better way of doing it
physics.start()physics.setGravity(0, 0)physics.setDrawMode( "normal" ) local snowball = display.newImage("snowball.png")snowball.x = 50snowball.y = display.stageHeight - 60physics.addBody( snowball, { density=0.3, friction=0.3, bounce=0.1, radius=20 } )snowball:addEventListener( "touch", dragBody )   local function dragBody( event )        physics.setGravity(0, 10)        gameUI.dragBody( event, { maxForce=55, frequency=5, dampingRatio=10} )end local function createSnowBall  -- probably doesnt need to be local.. refer to the original object instead  snowball = display.newImage("snowball.png")  snowball.x = 50  nowball.y = display.stageHeight - 60     snowball:addEventListener( "touch", dragBody )  endlocal function onCollision( event )        if event.object1 == floor then                  if ( event.phase == "began" ) then                        local oldsnowball = event.object2                                               oldsnowball:removeEventListenert("touch", dragBody)                        oldsnowball:removeSelf()                        createSnowBall()                end        endend

(altho note there is no real reason to destroy the snowball, if nothing is changing (physics/image) etc... just move it to a new position and reset velocities etc)
uid: 6645 topic_id: 3616 reply_id: 10996


[TOPIC: post.html]
#3

benlebt

[GLOBAL: userInfoPane.html]
benlebt
  • Enthusiast

  • 48 posts
  • Guests

Thanks a lot for your answer, I got it working now with re-initializing the ball. The problem seems to be that I have to wait a bit before reinitializing the ball and adding event listeners / physics to it. I've now put a timer in that recreates the ball some time after onCollision was called but not instantly.
I've tried not-reinitializing and simply moving the snowball to the original position before by setting its x and y values but that didn't work.

Another thing: as you can see I'm using gameUI for dragging the snowball (after drag it's released and then flies through the air). The game works nicely in the simulator with every possible device, whether android or iphone. But on the two android devices I got here (HTC myTouch, HTC Hero) I can't drag the snowball at all. It simply stands still on its starting point and doesn't move.
As if I would never release or move my finger. But the game recognizes that I'm pressing the screen cause the snowball is then "swinging" on the original point with the head down.
uid: 11219 topic_id: 3616 reply_id: 11001


[TOPIC: post.html]
#4

benlebt

[GLOBAL: userInfoPane.html]
benlebt
  • Enthusiast

  • 48 posts
  • Guests

OK i now found out that it must have something to do with the size of the background picture. if it's 1000x800 for example, the game will run very slow on android. i have to resize the picture to almost the original display size for the game to run smoothly.
let's see whether i can get gameui-drag running again with this setup, right now i'm using touch events and applyForce calls.
uid: 11219 topic_id: 3616 reply_id: 11007


[TOPIC: post.html]
#5

benlebt

[GLOBAL: userInfoPane.html]
benlebt
  • Enthusiast

  • 48 posts
  • Guests

OK i now found out that it must have something to do with the size of the background picture. if it's 1000x800 for example, the game will run very slow on android. i have to resize the picture to almost the original display size for the game to run smoothly.
let's see whether i can get gameui-drag running again with this setup, right now i'm using touch events and applyForce calls.
uid: 11219 topic_id: 3616 reply_id: 11008


[TOPIC: post.html]
#6

jmp909

[GLOBAL: userInfoPane.html]
jmp909
  • Corona Geek

  • 1,328 posts
  • Corona SDK

i think that is part of a noted bug on android ben
http://developer.anscamobile.com/issues/3150
uid: 6645 topic_id: 3616 reply_id: 11017


[TOPIC: post.html]
#7

benlebt

[GLOBAL: userInfoPane.html]
benlebt
  • Enthusiast

  • 48 posts
  • Guests

Ah ok, thanks.
Could be that I'm having another problem combined to this bug.
Right now I'm doing the following:

- the user touches the snowball
- with the "began" and "ended" events i calculate how long the user touched the snowball
- depending on the time the user touched it, the snowball is thrown with applyForce

--> each 4 or 5 throws on android, the time the user touched the snowball is way higher than i actually touched it. showing nearly 2 seconds of touching time although i only touched it for half a second or shorter.

--> seems to be that exact bug you mentioned?
uid: 11219 topic_id: 3616 reply_id: 11023


[TOPIC: post.html]
#8

jmp909

[GLOBAL: userInfoPane.html]
jmp909
  • Corona Geek

  • 1,328 posts
  • Corona SDK

don't expect touch events & physics to work on android at the moment.

this is definitely a problem as i tested the same app on an iphone 4 and it worked perfectly
uid: 6645 topic_id: 3616 reply_id: 11249



[topic_controls]
[/topic_controls]