Submit Your Article


 
RPG Maker

Welcome Guest ( Log In | Register )


  Games Resources RPG Maker VX RPG Maker XP Scripts Tutorials Downloads

 
Reply to this topicStart new topic
> Moving while doing animation loop
azdesign
post Jun 23 2012, 12:57 AM
Post #1



Group Icon

Group: Member
Posts: 2
Type: Artist
RM Skill: Beginner




Hello everyone, nice to meet you,
I made a custom class to display character portrait in RPGMaker XP Here is the class :

CODE
class Poser
  attr_accessor :images

  def initialize
    @images = Sprite.new
    @images.bitmap = RPG::Cache.picture('Character.png') #100x300
    @images.x = 540 #place it on the bottom right corner of the screen
    @images.y = 180
  end
end

Create an event on the map to create an instance as global variable, tada! image popped out. Ok nice. But Im not satisfied, Now I want it to have bobbing-head animation-like (just like when we breathe, sometimes bob our head up and down) so I added another method :

CODE
def move(x,y)
    @images.x += x
    @images.y += y
  end

  def animate(x,y,step,delay)
    forward = true
    2.times {
      step.times {
        wait(delay)
        if forward
          move(x/step,y/step)
        else
          move(-x/step,-y/step)
        end
      }
      wait(delay*3)
      forward = false
    }
  end

  def wait(time)
    while time > 0
      time -= 1
      Graphics.update
    end
  end

I called the method to run the animation and it works, so far so good, but the problem is, WHILE the portrait goes up and down, I cannot move my character until the animation is finished.

So that's it, I'm stuck in the loop block, what I want is to watch the portrait moving up and down while I walk around village, talk to npc, etc.

Anyone can solve my problem ? Or better solution ? Thanks in advance
Go to the top of the page
 
+Quote Post
   
Jens of Zanicuud
post Jun 24 2012, 06:10 AM
Post #2


Dark Jentleman
Group Icon

Group: Local Mod
Posts: 904
Type: Scripter
RM Skill: Skilled
Rev Points: 120




QUOTE (azdesign @ Jun 23 2012, 10:57 AM) *
Hello everyone, nice to meet you,
I made a custom class to display character portrait in RPGMaker XP Here is the class :

CODE
class Poser
  attr_accessor :images

  def initialize
    @images = Sprite.new
    @images.bitmap = RPG::Cache.picture('Character.png') #100x300
    @images.x = 540 #place it on the bottom right corner of the screen
    @images.y = 180
  end
end

Create an event on the map to create an instance as global variable, tada! image popped out. Ok nice. But Im not satisfied, Now I want it to have bobbing-head animation-like (just like when we breathe, sometimes bob our head up and down) so I added another method :

CODE
def move(x,y)
    @images.x += x
    @images.y += y
  end

  def animate(x,y,step,delay)
    forward = true
    2.times {
      step.times {
        wait(delay)
        if forward
          move(x/step,y/step)
        else
          move(-x/step,-y/step)
        end
      }
      wait(delay*3)
      forward = false
    }
  end

  def wait(time)
    while time > 0
      time -= 1
      Graphics.update
    end
  end

I called the method to run the animation and it works, so far so good, but the problem is, WHILE the portrait goes up and down, I cannot move my character until the animation is finished.

So that's it, I'm stuck in the loop block, what I want is to watch the portrait moving up and down while I walk around village, talk to npc, etc.

Anyone can solve my problem ? Or better solution ? Thanks in advance


Your while loop is the root of all evil smile.gif

Try use these methods, instead:

1. in the intiialize method, add this variable declaration:

CODE
@wait = 5
@step = 0
@anime_x = 0
@anime_y = 0
@forward = true
@repeat    = 0
@wait_count = 0


2. just modify this method your class:
CODE
def animate(x,y,step,delay)
@repeat = 2
@wait = delay
@wait_count = delay
@anime_x = x/step
@anime_y = y/step
@step = step
@forward = true
end


3. create this method in your class

CODE
def update
if @repeat > 0  #cycle
  if @wait > 0    #wait procedure
     @wait -= 1
     return
  end            #wait end
if @step > 0   #step procedure
   if @forward
  move(@anime_x,@anime_y)
else
  move(-@anime_x,-@anime_y)
  end            #end forward
@step -= 1
else
@forward = !@forward
@repeat -= 1
if @repeat < 0 #repeat occurence
@repeat = 4
@wait = 3*@wait_count
else
@wait = @wait_count
end       #end repeat
end      #end step
else
animate(x,y,steps,delay)
end     #end cycle

end    #end method


where x,y, step and delay are the variables you have to set at your will.

4. You must trigger an event in parallel process (or just a common event, as you wish... you can even modify Scene_Map's update, if you desire to...) and put this line into a call script:

CODE
$(variable).update


where $(variable) is your public instance object name...

and it should magically work (well, it should)  smile.gif

I just hope not to have done mistakes...
If so, just post smile.gif

I hope this can help...

Jens

This post has been edited by Jens of Zanicuud: Jun 24 2012, 06:13 AM


__________________________
"Thorns are the rose's sweetest essence..."
-Jens of Zanicuud


Games I'm working on:
>

official website: TryAdIne eFfeCt

>

Games I worked on (mainly as a scripter):
>
(Warning: it's a 3rr3's project and it's in Italian!)


Awards

Go to the top of the page
 
+Quote Post
   
azdesign
post Jun 24 2012, 06:29 AM
Post #3



Group Icon

Group: Member
Posts: 2
Type: Artist
RM Skill: Beginner




Thank you for your reply,
Your code works, but the problem is, animation only happen as I trigger the update method. Meaning I have to click click click click click click to run it
The purpose is how to let the animation run with a single call, and while it is running, we can still walk around ?

Currently, I found a way that actually works, but I don't know if its the most proper way. :

1. rather than create the instance of that class using event trigger, I wrote it in Scene_Map main method instead, so its automatically created upon loading any map.
2. Then, I called the update method inside main method's loop inside Scene_Map
3. Works, it animate in background, while I can do anything else

Do you have other suggestion ?

#EDIT : sorry, I didn't read your reply thoroughly. Indeed, you did mention to add it in Scene_Map , but I'm curious with "event in parallel process". I start learning ruby and RGSS a week ago and to be honest, never done eventing other than common event in event layer before. What is this "event in parallel process" and how its different with common event ?

This post has been edited by azdesign: Jun 24 2012, 06:40 AM
Go to the top of the page
 
+Quote Post
   
Jens of Zanicuud
post Jun 25 2012, 12:39 AM
Post #4


Dark Jentleman
Group Icon

Group: Local Mod
Posts: 904
Type: Scripter
RM Skill: Skilled
Rev Points: 120




A common event can be triggered with a switch without being physically present on the map;

An event triggered in parallel process has to be set on the map and works as a common event: it goes on repeating its instructions over and over, until you stop it with a switch control, variable control or anything else. An event triggered in parallel process affects only the map where it's located.

Auto-start event is different, since it will freeze the game to play its instructions until it's stopped manually (switch, self switch, variable).

While parallel process let you do something while active, auto-start has highest priority and you won't be allowed to do anything while active.

Jens

This post has been edited by Jens of Zanicuud: Jun 25 2012, 12:40 AM


__________________________
"Thorns are the rose's sweetest essence..."
-Jens of Zanicuud


Games I'm working on:
>

official website: TryAdIne eFfeCt

>

Games I worked on (mainly as a scripter):
>
(Warning: it's a 3rr3's project and it's in Italian!)


Awards

Go to the top of the page
 
+Quote Post
   

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 21st May 2013 - 04:21 PM
RPG RPG Revolution is an Privacy Policy and Legal
eXTReMe Tracker