motion/joybox/core/sprite.rb in joybox-1.0.0 vs motion/joybox/core/sprite.rb in joybox-1.1.0

- old
+ new

@@ -1,31 +1,35 @@ module Joybox module Core class Sprite < CCSprite - include Joybox::Common + extend Joybox::Common::Initialize - alias_method :run_action, :runAction - alias_method :stop_action, :stopAction - alias_method :stop_all_actions, :stopAllActions alias_method :bounding_box, :boundingBox - - - def self.new(options = {}) - sprite ||= new_with_file_name(options) if options.has_key? (:file_name) - sprite ||= new_with_texture(options) if options.has_key? (:texture) - sprite ||= new_with_frame_name(options) if options.has_key? (:frame_name) - - sprite.position = options[:position] if options.has_key? (:position) - sprite + alias_method :frame, :displayFrame + alias_method :frame=, :setDisplayFrame + attr_accessor :properties + + def initialize(options = {}) + initialize_with_file_name(options) if options.has_key? (:file_name) + initialize_with_texture(options) if options.has_key? (:texture) + initialize_with_frame_name(options) if options.has_key? (:frame_name) + initialize_with_frame(options) if options.has_key? (:frame) + initialize_with_cg_image(options) if options.has_key?(:cg_image) && options.has_key?(:key) + self.position = options.delete(:position) if options.has_key? (:position) + self.properties = options end - + def file_name=(file_name) texture = CCTextureCache.sharedTextureCache.addImage(file_name) self.setTexture(texture) end + + def displays_frame?(frame) + isFrameDisplayed(frame) + end # Review this in another version, because I dont still quite convinced # that this is the correct approach. The alternative is two methods # flip_x and flip_y, but I dont like them either. def flip(options = {}) @@ -47,27 +51,41 @@ def boundingBox super end + def [](key) + @properties[key] + end + + def []=(key, value) + @properties[key] = value + end + private - def self.new_with_file_name(options = {}) - spriteWithFile(options[:file_name]) + def initialize_with_file_name(options = {}) + initWithFile(options.delete(:file_name)) if options[:rect].nil? + initWithFile(options.delete(:file_name), rect: options.delete(:rect)) unless options[:rect].nil? end - def self.new_with_texture(options = {}) - if options.has_key? (:rect) - spriteWithTexture(options[:texture], rect: options[:rect]) - else - spriteWithTexture(options:[:texture]) - end + def initialize_with_texture(options = {}) + initWithTexture(options.delete(:texture)) if options[:rect].nil? + initWithTexture(options.delete(:texture), rect: options.delete(:rect)) unless options[:rect].nil? end - def self.new_with_frame_name(options = {}) - spriteWithSpriteFrameName(options[:frame_name]) + def initialize_with_frame_name(options = {}) + initWithSpriteFrameName(options.delete(:frame_name)) end + def initialize_with_frame(options = {}) + initWithSpriteFrame(options.delete(:frame)) + end + + def initialize_with_cg_image(options = {}) + initWithCGImage(options.delete(:cg_image).CGImage, key: options.delete(:key)) + end + end end -end \ No newline at end of file +end