sprite or object?

hi everybody,

I am making a platform game and i want to create different enemies, so each enemy created has his own properties in the world.

as a test a have done this.

Here i add 20 enemies

[lua]

-CREATE ENEMIES

local spriteSheet = graphics.newImageSheet(“images/mouseSprite.png”, {width = 70, height = 50, numFrames = 4})

local sequenceData = {

        {name = “walkRight”, sheet = spriteSheet, frames = {1,2,3,4}, time = 400, loopCount = 0},

        

    }

    local mouse,setup1

for i=1, 20 do            

     mouse = display.newSprite(spriteSheet, sequenceData)

     setup1 = {layer = 6, kind = “imageRect”, locX =locX, locY = 75.72, 

    levelWidth = 70, levelHeight = 50, offsetX = 0, offsetY = 0    }

    mte.addSprite(mouse, setup1)

    mouse:setSequence( “walkRight” )

                mouse:play()

    --mouse.x=192;mouse.y=1150

    mouse.speed= 1.5;mouse.name=“mouse”

    mouse.allowedMovement=200

    mouse.travelled=0

    locX=locX+2

    

    print(locX)

end

[/lua]

now at the EnterFrame, i want to move and check the positions, 

[lua]

enterframe…

for i=1, 20 do

    

    

    mte.moveSprite(mouse(i), mouse.speed, 0)    

    mouse.travelled = mouse.travelled + mouse.speed

    if mouse.speed < 0 then 

    if mouse.xScale < 0 then mouse.xScale = 1 end

    else

    if mouse.xScale > 0 then mouse.xScale = -1 end

    end

                        if mouse.travelled >= mouse.allowedMovement or mouse.travelled <= -mouse.allowedMovement then

                            mouse.speed = -mouse.speed 

                            mouse.travelled = 0

                        end

    end                    

[/lua]

Here i have the problem.When i add the sprite to the mte.world, the other propertiers like “mouse.allowedMovement” or mouse.name" etc that i need to retieve for each sprite were not added, so i can´t get them.

if i add enemies,or coins as an object would be a better way than as Sprites? is there another way easier? 

thank you very much

Txarly

Hello Txarly

Your code creates twenty sprites but reuses the same variable (mouse) for storing the reference to them. The end result is that you will only be able to reach the last mouse you created. 

Try turning mouse into an array.

local mouse = {} --Create array local setup1 for i=1, 20 do mouse[i] = display.newSprite(spriteSheet, sequenceData) setup1 = {layer = 6, kind = "imageRect", locX =locX, locY = 75.72, levelWidth = 70, levelHeight = 50, offsetX = 0, offsetY = 0 } mte.addSprite(mouse[i], setup1) mouse[i]:setSequence( "walkRight" ) mouse[i]:play() --mouse[i].x=192;mouse[i].y=1150 mouse[i].speed= 1.5;mouse[i].name="mouse "..i mouse[i].allowedMovement=200 mouse[i].travelled=0 locX=locX+2 print(locX) end

You should now be able to access each mouse’s own parameters such as name, etc. One thing to keep in mind is that the proper way to access a position in an array is with square brackets. Parenthesis won’t work correctly. mouse(i) should be mouse[i].

for i=1, 20 do mte.moveSprite(mouse[i], mouse[i].speed, 0) mouse[i].travelled = mouse[i].travelled + mouse[i].speed if mouse[i].speed \< 0 then if mouse[i].xScale \< 0 then mouse[i].xScale = 1 end else if mouse[i].xScale \> 0 then mouse[i].xScale = -1 end end if mouse[i].travelled \>= mouse[i].allowedMovement or mouse[i].travelled \<= -mouse[i].allowedMovement then mouse[i].speed = -mouse[i].speed mouse[i].travelled = 0 end end 

Hope that helps.

thanks dyson , do you think you will have the physics ready for the engine  soon?

I’ll be releasing an update including the current progress on physics integration at the end of next week! From there I’ll continue to tweak and flesh it out as suggestions and requests come in.

Hello Txarly

Your code creates twenty sprites but reuses the same variable (mouse) for storing the reference to them. The end result is that you will only be able to reach the last mouse you created. 

Try turning mouse into an array.

local mouse = {} --Create array local setup1 for i=1, 20 do mouse[i] = display.newSprite(spriteSheet, sequenceData) setup1 = {layer = 6, kind = "imageRect", locX =locX, locY = 75.72, levelWidth = 70, levelHeight = 50, offsetX = 0, offsetY = 0 } mte.addSprite(mouse[i], setup1) mouse[i]:setSequence( "walkRight" ) mouse[i]:play() --mouse[i].x=192;mouse[i].y=1150 mouse[i].speed= 1.5;mouse[i].name="mouse "..i mouse[i].allowedMovement=200 mouse[i].travelled=0 locX=locX+2 print(locX) end

You should now be able to access each mouse’s own parameters such as name, etc. One thing to keep in mind is that the proper way to access a position in an array is with square brackets. Parenthesis won’t work correctly. mouse(i) should be mouse[i].

for i=1, 20 do mte.moveSprite(mouse[i], mouse[i].speed, 0) mouse[i].travelled = mouse[i].travelled + mouse[i].speed if mouse[i].speed \< 0 then if mouse[i].xScale \< 0 then mouse[i].xScale = 1 end else if mouse[i].xScale \> 0 then mouse[i].xScale = -1 end end if mouse[i].travelled \>= mouse[i].allowedMovement or mouse[i].travelled \<= -mouse[i].allowedMovement then mouse[i].speed = -mouse[i].speed mouse[i].travelled = 0 end end 

Hope that helps.

thanks dyson , do you think you will have the physics ready for the engine  soon?

I’ll be releasing an update including the current progress on physics integration at the end of next week! From there I’ll continue to tweak and flesh it out as suggestions and requests come in.