Is there any common known reason to why new coordinates given to sprites don't update on the screen? Naturally, I've checked that my coordinates update exactly as intended, but I don't see my sprites changing their positions visually on the screen at all.
I'm keeping a solid 40 FPS, so Graphics.update is definitely being called properly. I've checked that the methods in question are definitely being run at a constant rate by printing the details.
Below are the methods currently in question.
The "@player" variable here refers to my Player class, which is the main sprite object that inherits RPG::Sprite, but as its coordinates dynamically changes, and I've proven so by outputting the coordinates into a console repeatedly, the sprite's position visually on the screen stays the same. Here's the player class (note it's completely unfinished, and I'm no master in coding. I tend to fix things as I go, right now I only wish to fix the graphic coordinate issue):
The methods that refer to the "Engine" module returns the boolean values I intend them to return, so that isn't very important to include here.
Anyone got any ideas on what I am doing wrong?
Thanks!
I'm keeping a solid 40 FPS, so Graphics.update is definitely being called properly. I've checked that the methods in question are definitely being run at a constant rate by printing the details.
Below are the methods currently in question.
Code:
def render
# Update necessary modules
Graphics.update
Input.update
# Update player
@player.update
# Update all sprites
@spriteset.tiles.each {|tile| tile.update}
end
Code:
#============================================================================
# ** Player
#----------------------------------------------------------------------------
# This class contains the player sprite and its functions.
#============================================================================
class Player < RPG::Sprite
#--------------------------------------------------------------------------
# * Constants
#--------------------------------------------------------------------------
Left = 0 ; Right = 1
#--------------------------------------------------------------------------
# * Public Instance Variables
#--------------------------------------------------------------------------
attr_accessor :dir
#--------------------------------------------------------------------------
# * Object Initialization
# graphic : string name of the character graphic
#--------------------------------------------------------------------------
def initialize(graphic)
super()
# Set bitmap
self.bitmap = RPG::Cache.character(graphic,0)
# Set coordinates
self.x = 96
self.y = 288
self.z = (Game::PlayerLayer * 500) + 250
width = self.bitmap.width / 4
height = self.bitmap.height / 4
self.src_rect.set(0,0,width,height)
# Set variables
@dir = Right
@step = 0
@moving = false
@jumping= false
@falling= false
@tiles = 0
@route = nil
# Set destination coordinates
@destination = nil
# Set source rectangle
set_graphic
end
#--------------------------------------------------------------------------
# * Update
#--------------------------------------------------------------------------
def update
return unless self.visible
super
# Set graphic
set_graphic
# If moving: update movement
update_movement if moving?
# Return if processing
return if Game.processing?
# If there's no tile under the player
if Engine.scene.passable?(self.tile_x,self.tile_y+1) and !@jumping
@falling = true
@moving = true
return
elsif !Engine.scene.passable?(self.tile_x,self.tile_y+1) and @falling
@falling = false
@moving = false
return
end
# If jump key and not jumping
if Input.trigger?(Game::JumpKey) and !@jumping
jump
end
# Check for movement keys
if Input.press?(Input::LEFT)
move_left
return
elsif Input.press?(Input::RIGHT)
move_right
return
end
end
#--------------------------------------------------------------------------
# * Update Movement
#--------------------------------------------------------------------------
def update_movement
# If destination isn't nil
unless @destination.nil?
# If destination has been reached
if [self.tile_x,self.tile_y] == @destination
@destination = nil
@route = nil
@step = 0
else
case @dir
when Left
self.x -= 4
when Right
self.x += 4
end
unless (@jumping or @falling)
@step += 1
@step %= 4
end
end
return
end
# When jumping
if @jumping
if Engine.scene.passable?(self.tile_x,self.tile_y-1) and
![self.tile_x,self.tile_y] == @tiles
self.y -= 8
else
@falling = true
@jumping = false
end
end
# When falling
if @falling
if Engine.scene.passable?(self.tile_x,self.tile_y+1)
self.y += 8
else
@falling = false
end
end
# If route isn't empty
unless @route.nil?
if @route == "move_left"
if Engine.scene.passable?(self.tile_x-1,self.tile_y)
@destination = [self.tile_x-1,self.tile_y]
@dir = Left
else
@route = nil
end
elsif @route == "move_right"
if Engine.scene.passable?(self.tile_x+1,self.tile_y)
@destination = [self.tile_x+1,self.tile_y]
@dir = Right
else
@route = nil
end
end
return
end
# If not falling or jumping: set moving off
unless (@falling and @jumping)
@moving = false
end
end
#--------------------------------------------------------------------------
# * Move Left
#--------------------------------------------------------------------------
def move_left
return unless @route.nil?
@route = 'move_left'
@moving = true
end
#--------------------------------------------------------------------------
# * Move Right
#--------------------------------------------------------------------------
def move_right
return unless @route.nil?
@route = 'move_right'
@moving = true
end
#--------------------------------------------------------------------------
# * Jump
#--------------------------------------------------------------------------
def jump
@tiles = [self.tile_x,self.tile_y-3]
@moving = true
@jumping = true
end
#--------------------------------------------------------------------------
# * Moving?
#--------------------------------------------------------------------------
def moving?
@moving
end
#--------------------------------------------------------------------------
# * Tile X
#--------------------------------------------------------------------------
def tile_x
self.x / 32
end
#--------------------------------------------------------------------------
# * Tile Y
#--------------------------------------------------------------------------
def tile_y
self.y / 32
end
#--------------------------------------------------------------------------
# * Set Graphic
#--------------------------------------------------------------------------
def set_graphic
return unless self.visible
# Obtain width and height of frames
width = self.bitmap.width / 4
height = self.bitmap.height / 4
self.ox = width / 2
self.oy = height - 32
# Set ch
ch = (@jumping or @falling) ? height * 2 : 0
# Direction case
case @dir
when Left
self.src_rect.set(@step * width,ch,width,height)
when Right
self.src_rect.set(@step * width,height+ch,width,height)
end
end
end
Anyone got any ideas on what I am doing wrong?
Thanks!