animation movement help

23 replies [Last post]
shakmbakm
User offline. Last seen 6 weeks 4 days ago. Offline
Joined: 28 Sep 2011

I am currently working on animating an enemy. While i have the animation working, I need it moving and it is not. I don't know why. Here is the code: any help would be great, thanks!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
        -- tank animation
        local sheet1 = sprite.newSpriteSheet( "tank_sprite_sheet.png", 75, 125)
 
        local spriteSet1 = sprite.newSpriteSet(sheet1, 1, 5)
        sprite.add( spriteSet1, "tank", 1, 5, 1000, 0 ) -- plays non shooting frames every 1000 ms
 
        local instance1 = sprite.newSprite( spriteSet1 )
        instance1.x = display.contentWidth / 75
        instance1.y = 125
        --instance1.xScale = .5
        --instance1.yScale = .5
 
        instance1:prepare("tank")
        instance1:play()
        
        -- A per-frame event to move the elements
        local tPrevious = system.getTimer()
        local function move(event)
        local tDelta = event.time - tPrevious
        tPrevious = event.time
        
        --if (grass.x + grass.contentWidth) < 0 then
        --      grass:translate( 480 * 2, 0)
        --end
 
        tank = {}
        
        for i = 1, #tank, 1 do
                tank[i].x = tank[i].x - tank[i].dx * tDelta * 0.2
                if (tank[i].x + tank[i].contentWidth) < 0 then
                        tank[i]:translate( 480 + tank[i].contentWidth * 2, 0 )
                end
        end
end

Replies

peach pellen's picture
peach pellen
User offline. Last seen 1 day 13 hours ago. Offline
Staff
Joined: 12 Apr 2011

Where are your tanks? I see you doing tank = {} but not actually adding any tanks to it.

shakmbakm
User offline. Last seen 6 weeks 4 days ago. Offline
Joined: 28 Sep 2011

is that why it is animating but not moving then? because no tanks are being added?

peach pellen's picture
peach pellen
User offline. Last seen 1 day 13 hours ago. Offline
Staff
Joined: 12 Apr 2011

Your code is written to move tanks, however many tanks you have in the tank table - but the table is empty so there is nothing to move.

shakmbakm
User offline. Last seen 6 weeks 4 days ago. Offline
Joined: 28 Sep 2011

ah ok, i see what you mean. thanks!

shakmbakm
User offline. Last seen 6 weeks 4 days ago. Offline
Joined: 28 Sep 2011

the table has been scraped now, we are trying to call the start point and move the tank from the same function but it is not cooperating. Half the time is says setStartPoint is a nil value and half the time it says instance1 is a nil value in the rotation line.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
 local function tank(event)
 
-- tank animation
        local sheet1 = sprite.newSpriteSheet( "tank_sprite_sheet.png", 75, 125)
        
 
 
        local spriteSet1 = sprite.newSpriteSet(sheet1, 1, 5)
        sprite.add( spriteSet1, "tank", 1, 5, 1000, 0 ) -- plays non shooting frames every 1000 ms
        
function setStartPoint(x, y)
 
        local instance1 = sprite.newSprite( spriteSet1 )
        instance1.x = x 
        instance1.y = y + 5
 
        --instance1.xScale = .5
        --instance1.yScale = .5
        
        
        --instance1.x = x
        --instance1.y = y
--end
        
        local tank = instance1.rotation
        instance1.rotation = 90
        end
        instance1:prepare("tank")
        instance1:play()
end
        
        --if(self.x = rich.x)
        --fire cannon
--if(self.x > rich.x) or (self.x < rich.x)
 --then instance1.x + 5 until = rich.x
--end
 
Runtime:addEventListener("enterFrame", tank)

shakmbakm
User offline. Last seen 6 weeks 4 days ago. Offline
Joined: 28 Sep 2011

someone else who uses a mac tried it out and had no errors but the tank was not moving as well.

peach pellen's picture
peach pellen
User offline. Last seen 1 day 13 hours ago. Offline
Staff
Joined: 12 Apr 2011

You have two things called "tank", a function and an object, you may want to remedy that :)

shakmbakm
User offline. Last seen 6 weeks 4 days ago. Offline
Joined: 28 Sep 2011

i changed the name of the function and that fixed part of it but now it is saying that instance1 is a nil value on instance1:prepare(tank) line

peach pellen's picture
peach pellen
User offline. Last seen 1 day 13 hours ago. Offline
Staff
Joined: 12 Apr 2011

Take a look at the sample code located;
CoronaSDK > SampleCode > Sprites > JungleScene

If you do that and can't modify it for your sprite, post code again, with all current changes, and I can try to advise further.

Peach :)

shakmbakm
User offline. Last seen 6 weeks 4 days ago. Offline
Joined: 28 Sep 2011

my code is similar to that but that code is not in a function, here is my current code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
 module(..., package.seeall)
require "sprite"
 
 
local function tankmove(event)
 
-- tank animation
        local sheet1 = sprite.newSpriteSheet( "tank_sprite_sheet.png", 75, 125)
        
 
 
        local spriteSet1 = sprite.newSpriteSet(sheet1, 1, 5)
        sprite.add( spriteSet1, "tank", 1, 5, 1000, 0 ) -- plays non shooting frames every 1000 ms
        
function setStartPoint(x, y)
 
        local instance1 = sprite.newSprite( spriteSet1 )
        instance1.x = x 
        instance1.y = y + 5
 
        --instance1.xScale = .5
        --instance1.yScale = .5
        
        
        --instance1.x = x
        --instance1.y = y
--end
        
        local tank = instance1.rotation
        instance1.rotation = 90
        end
        instance1:prepare("tank")
        instance1:play()
end
        
        --if(self.x = rich.x)
        --fire cannon
--if(self.x > rich.x) or (self.x < rich.x)
 --then instance1.x + 5 until = rich.x
--end
 
Runtime:addEventListener("enterFrame", tankmove)

peach pellen's picture
peach pellen
User offline. Last seen 1 day 13 hours ago. Offline
Staff
Joined: 12 Apr 2011

Why is this tied to a Runtime listener?

Can you move everything there that doesn't need to be within that function outside of it? I expect that is causing the error.

OOI, what part of that function are you trying to use every frame? Might have some advise there also.

Peach :)

shakmbakm
User offline. Last seen 6 weeks 4 days ago. Offline
Joined: 28 Sep 2011

the instance, lines 17-19 and 32 and 33 are also relevant. Those are what need to be checked every frame for movement. I thought connecting a listener to the function would work but its only partially working. Maybe runtime was the wrong kind of listener to use?

shakmbakm
User offline. Last seen 6 weeks 4 days ago. Offline
Joined: 28 Sep 2011

i took function off of set start point where then it was saying that set start point was a nil value. I think the listener should be set to check the setstartpoint function.

shakmbakm
User offline. Last seen 6 weeks 4 days ago. Offline
Joined: 28 Sep 2011

i think i'm close, it said assertion failed. thats all it said though. I'm not sure what that means

peach pellen's picture
peach pellen
User offline. Last seen 1 day 13 hours ago. Offline
Staff
Joined: 12 Apr 2011

The code above isn't calling setStartPoint, it's just reloading the sprite every frame, when it only needs to be loaded one time.

Without seeing the setStartPoint being called it is hard to know quite what is happening, how, when, etc.

shakmbakm
User offline. Last seen 6 weeks 4 days ago. Offline
Joined: 28 Sep 2011

right so it needs to call the setStartPoint. what should be put in the listener? x,y? That is what i tried and it said assertion failed.

peach pellen's picture
peach pellen
User offline. Last seen 1 day 13 hours ago. Offline
Staff
Joined: 12 Apr 2011

setStartPoint(10,10)

With 10, 10 being the X and Y you want to use.

shakmbakm
User offline. Last seen 6 weeks 4 days ago. Offline
Joined: 28 Sep 2011

should the setStartPoint be in the listener as well?

shakmbakm
User offline. Last seen 6 weeks 4 days ago. Offline
Joined: 28 Sep 2011

with x,y it said assertion failed and when i put in 10,10 it was expecting a name. I have it calling the setStartPoint function now.

shakmbakm
User offline. Last seen 6 weeks 4 days ago. Offline
Joined: 28 Sep 2011

I noticed its saying assertion failed for the level its being called for, which means there is something wrong with the level code then. The only thing is that I can't see why it would be saying assertion failed. here's the code for that

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
 module(..., package.seeall)
 
--_G.playWorld1Soundtrack()
--timer.cancel
 
require "sprite"
 
if world1Soundtrack then
timer.cancel(_G.bbbbbb)
_G.bbbbbb = nil
else
_G.playWorld1Soundtrack()
--_G.bbbbbb = timer.performWithDelay(1, _G. playWorld1Soundtrack, 0)
end
 
--local world1Soundtrack = audio.loadStream("AClimber_World_1.mp3")
--local world1SoundtrackOn = false
 
--function music()
        --if (world1SoundtrackOn == false) then
                --world1SoundtrackOn = true
                --audio.play(world1Soundtrack)
        --elseif (world1SoundtrackOn == true) then
        --end
--end
--music()
 
function new()
        local localGroup = display.newGroup()
        
        local background = display.newImageRect('1-1.png', 320, 480, true)
        background.x = display.contentWidth / 2
        background.y = display.contentHeight / 2
        
        --local myText = display.newTextRect( "Area Complete!", 0, 0, "Helvetica", 16 )
        --myText:setTextColor(0, 0, 255)
        
        local endButton = display.newImageRect('endbutton.png', 22, 44, true)
        endButton:setReferencePoint(display.CenterReferencePoint)
        endButton.x = 228
        endButton.y = 22
        endButton.scene = "onetwo"
        
 
        local tank = require "tank"
        tank.setStartPoint(35, 30)
        --print("occured")

WauloK's picture
WauloK
User offline. Last seen 3 days 7 hours ago. Offline
Joined: 21 Oct 2010

Do you have a copy of the error?

shakmbakm
User offline. Last seen 6 weeks 4 days ago. Offline
Joined: 28 Sep 2011

the error says:
Director ERROR: failed to execute new( params ) function on oneone
-------------------
assertion failed!

peach pellen's picture
peach pellen
User offline. Last seen 1 day 13 hours ago. Offline
Staff
Joined: 12 Apr 2011

You don't seem to be inserting your objects into localGroup in the above code.

Viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.