Timeshifting and Pausing PathModifier

  ... the case you feel the need for a new feature or want to submit one.

Timeshifting and Pausing PathModifier

Postby tajny » Mon Aug 01, 2011 7:06 pm

Hi, in case someone need a quick way to pause pathmodifier or change speed of it here is modified PathModifier that will do the job.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.axl.extras;
  2.  
  3. import org.anddev.andengine.entity.IEntity;
  4. import org.anddev.andengine.entity.modifier.PathModifier;
  5. import org.anddev.andengine.util.modifier.ease.IEaseFunction;
  6.  
  7. public class ExtendedPathModifier extends PathModifier {
  8.  
  9.         private boolean mStatePaused = false;
  10.         private float   mTimeModifier = 1f;
  11.        
  12.         private IExtendedPathModifierListener mExtendedPathModifierListener;
  13.        
  14.         public ExtendedPathModifier(final float pDuration, final Path pPath, final IEntityModifierListener pEntityModiferListener, final IPathModifierListener pPathModifierListener,final IExtendedPathModifierListener pExtendedPathModifierListener, final IEaseFunction pEaseFunction) throws IllegalArgumentException {
  15.                 super(pDuration,pPath,pEntityModiferListener,pPathModifierListener,pEaseFunction);
  16.                 mExtendedPathModifierListener = pExtendedPathModifierListener;
  17.         }
  18.        
  19.         public ExtendedPathModifier(float pDuration, Path pPath) {
  20.                 super(pDuration, pPath);
  21.         }
  22.        
  23.         public void pauseModifier(){
  24.                 if (!this.mStatePaused) {
  25.                         this.mExtendedPathModifierListener.onPause(this);
  26.                         this.mStatePaused = true;
  27.                 }
  28.         }
  29.        
  30.         public void resumeModifier(){
  31.                 if (!this.mStatePaused){
  32.                         this.mExtendedPathModifierListener.onResume(this);
  33.                         this.mStatePaused = false;
  34.                 }
  35.         }
  36.        
  37.         public void setTimeModifier(final float pTimeModifier){
  38.                 mTimeModifier = pTimeModifier;
  39.         }
  40.        
  41.         @Override
  42.         public float onUpdate(final float pSecondsElapsed, final IEntity pEntity) {
  43.                 if (mStatePaused) return 0f;
  44.                 if (mTimeModifier!=1f) return super.onUpdate(pSecondsElapsed*mTimeModifier, pEntity);
  45.                 return super.onUpdate(pSecondsElapsed, pEntity);
  46.         }
  47.        
  48.  
  49.         public static interface IExtendedPathModifierListener {
  50.                 // ===========================================================
  51.                 // Constants
  52.                 // ===========================================================
  53.        
  54.                 // ===========================================================
  55.                 // Fields
  56.                 // ===========================================================
  57.        
  58.                 public void onPause(final PathModifier pPathModifier);
  59.                 public void onResume(final PathModifier pPathModifier);
  60.         }
  61. }
  62.  
Parsed in 0.039 seconds, using GeSHi 1.0.8.4


usage is similar to normal pathmodifier with that difference you can call three methods :
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. pauseModifier();
  2. resumeModifier();
  3. setTimeModifier(final float pTimeModifier);
  4.  
Parsed in 0.030 seconds, using GeSHi 1.0.8.4


when called setTimerModifier with for example 2f , sprite will follow the path 2 times faster, and when set 0.5f two times slower.

I'm using it to freeze my enemies or slow them down.

note that you could slow down / speed up all updatehandlers and entitymodifiers with changing pSecondsElapsedin onmanagedupdate of sprite like so :
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. @Override
  2. protected void onManagedUpdate(final float pSecondsElapsed) {
  3.         super.onManagedUpdate(pSecondsElapsed*0.5f);
  4. }
  5.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4

Paweł P.

--------------------------------------
Portfolio

Run Santa Run Link
Xelorians Link
User avatar
tajny
 
Posts: 216
Joined: Mon May 30, 2011 10:23 pm
Location: Poland

Re: Timeshifting and Pausing PathModifier

Postby brbzull » Fri Oct 28, 2011 4:03 am

Hi, I´d try something similar, just for pausing, not for changing the time.. and I found that, when toy stop the modifier works fine, but when you restart it, looks like the shape you are moving continue to faster. I think there are some tweaking we have to do with the endtime.. I think I can't access to the duration or end time.

Damian.
brbzull
 
Posts: 15
Joined: Tue May 31, 2011 6:25 pm

Re: Timeshifting and Pausing PathModifier

Postby johnas » Tue Jan 17, 2012 4:52 pm

Hello I have the folowing code for a sprite to follow a path
I have tried to change the speed of the sprites by bodging
the path() to move back a bit which kind of works.
Could you please tell me how I can adapt this to use your class
to alter the velocity of sprites.
thanks John


private void follow_path5(final AnimatedSprite tSprite,int i) {
final Path path = new Path(17)
.to(20, startY)
.to(250, startY)
.to(235, startY)
.to(230, startY)
.to(416.0f, startY)
.to(300.0f, startY)
.to(285.0f, startY)
.to(270.0f, startY)
.to(20.0f, startY)
.to(157, startY)
.to(145, startY)
.to(139, startY)
.to(416.0f,startY)
.to(409.0f, startY)
.to(400.0f, startY)
.to(350.0f, startY)
.to(20.0f,startY);


// Add the proper animation when a waypoint of the path is passed.
tSprite.registerEntityModifier(new PathModifier(30.0f, path, null, new IPathModifierListener() {
@Override
public void onPathStarted(final PathModifier pPathModifier, final IEntity pEntity) {

}

@Override
public void onPathWaypointStarted(final PathModifier pPathModifier, final IEntity pEntity, final int pWaypointIndex) {

switch(pWaypointIndex) {
case 0: case 1: case 2: case 3:
final long[] frameDurations = new long[3];
Arrays.fill(frameDurations, 500);
tSprite.animate(frameDurations, 2, 4, true);

break;

case 4: case 5: case 6: case 7:
final long[] frameDurations1 = new long[3];
Arrays.fill(frameDurations1, 500);
tSprite.animate(frameDurations1, 5, 7, true);

break;

case 8: case 9: case 10: case 11:
final long[] frameDurations2 = new long[3];
Arrays.fill(frameDurations2, 500);
tSprite.animate(frameDurations2, 2, 4, true);

break;

case 12: case 13: case 14:
final long[] frameDurations3 = new long[3];
Arrays.fill(frameDurations3, 500);
tSprite.animate(frameDurations3, 5, 7, true);

break;
}
}

@Override
public void onPathWaypointFinished(final PathModifier pPathModifier, final IEntity pEntity, final int pWaypointIndex) {

}

@Override
public void onPathFinished(final PathModifier pPathModifier, final IEntity pEntity) {
}));
scene.attachChild(asprVamp[i]);
johnas
 
Posts: 10
Joined: Mon Jan 16, 2012 3:06 pm

Re: Timeshifting and Pausing PathModifier

Postby sgogogo » Sat Sep 29, 2012 10:58 pm

tajny wrote:Hi, in case someone need a quick way to pause pathmodifier or change speed of it here is modified PathModifier that will do the job.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.axl.extras;
  2.  
  3. import org.anddev.andengine.entity.IEntity;
  4. import org.anddev.andengine.entity.modifier.PathModifier;
  5. import org.anddev.andengine.util.modifier.ease.IEaseFunction;
  6.  
  7. public class ExtendedPathModifier extends PathModifier {
  8.  
  9.         private boolean mStatePaused = false;
  10.         private float   mTimeModifier = 1f;
  11.        
  12.         private IExtendedPathModifierListener mExtendedPathModifierListener;
  13.        
  14.         public ExtendedPathModifier(final float pDuration, final Path pPath, final IEntityModifierListener pEntityModiferListener, final IPathModifierListener pPathModifierListener,final IExtendedPathModifierListener pExtendedPathModifierListener, final IEaseFunction pEaseFunction) throws IllegalArgumentException {
  15.                 super(pDuration,pPath,pEntityModiferListener,pPathModifierListener,pEaseFunction);
  16.                 mExtendedPathModifierListener = pExtendedPathModifierListener;
  17.         }
  18.        
  19.         public ExtendedPathModifier(float pDuration, Path pPath) {
  20.                 super(pDuration, pPath);
  21.         }
  22.        
  23.         public void pauseModifier(){
  24.                 if (!this.mStatePaused) {
  25.                         this.mExtendedPathModifierListener.onPause(this);
  26.                         this.mStatePaused = true;
  27.                 }
  28.         }
  29.        
  30.         public void resumeModifier(){
  31.                 if (!this.mStatePaused){
  32.                         this.mExtendedPathModifierListener.onResume(this);
  33.                         this.mStatePaused = false;
  34.                 }
  35.         }
  36.        
  37.         public void setTimeModifier(final float pTimeModifier){
  38.                 mTimeModifier = pTimeModifier;
  39.         }
  40.        
  41.         @Override
  42.         public float onUpdate(final float pSecondsElapsed, final IEntity pEntity) {
  43.                 if (mStatePaused) return 0f;
  44.                 if (mTimeModifier!=1f) return super.onUpdate(pSecondsElapsed*mTimeModifier, pEntity);
  45.                 return super.onUpdate(pSecondsElapsed, pEntity);
  46.         }
  47.        
  48.  
  49.         public static interface IExtendedPathModifierListener {
  50.                 // ===========================================================
  51.                 // Constants
  52.                 // ===========================================================
  53.        
  54.                 // ===========================================================
  55.                 // Fields
  56.                 // ===========================================================
  57.        
  58.                 public void onPause(final PathModifier pPathModifier);
  59.                 public void onResume(final PathModifier pPathModifier);
  60.         }
  61. }
  62.  
Parsed in 0.043 seconds, using GeSHi 1.0.8.4


usage is similar to normal pathmodifier with that difference you can call three methods :
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. pauseModifier();
  2. resumeModifier();
  3. setTimeModifier(final float pTimeModifier);
  4.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


when called setTimerModifier with for example 2f , sprite will follow the path 2 times faster, and when set 0.5f two times slower.

I'm using it to freeze my enemies or slow them down.

note that you could slow down / speed up all updatehandlers and entitymodifiers with changing pSecondsElapsedin onmanagedupdate of sprite like so :
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. @Override
  2. protected void onManagedUpdate(final float pSecondsElapsed) {
  3.         super.onManagedUpdate(pSecondsElapsed*0.5f);
  4. }
  5.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4



Hello, I am trying to implement your class extender ExtendedPathModifier in Sprite path movement but I can't access to pauseModifier(); or resumeModifier(); methods, I only can see interface methods onPause and onResume.
What is the correct way to pause the sprite when it reaches a IPathModifierListener point (IPathModifierListener for example)?

Any help would be greatly appreciated

Thanks and regards
sgogogo
 
Posts: 15
Joined: Sat Nov 19, 2011 3:04 pm

Re: Timeshifting and Pausing PathModifier

Postby shihab_returns » Tue Feb 19, 2013 12:23 pm

@tanjy

great ...like it most...please continue your great works. :D
shihab_returns
 
Posts: 101
Joined: Tue Oct 16, 2012 8:48 am
Location: Dhaka

Re: Timeshifting and Pausing PathModifier

Postby raphael » Sat Apr 20, 2013 5:31 pm

I use this but all the game is paused the game stop completely in below of 0.6f, more speed work .... i andengine GLES 1

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. final AnimatedSprite papillon1 = new AnimatedSprite(1839, 884, mButterflyTextureRegion.clone());
  2.        
  3.         final Path path2 = new Path(3).to(1839, 884).to(1135, 895).to( 1839, 884);
  4.         //207
  5.        
  6.         //new DelayModifier(10, new IEntityModifierListener() {
  7.  
  8.         PathExtendedPathModifier =                             
  9.                                 new ExtendedPathModifier(35, path2, null, new IPathModifierListener() {
  10.  
  11.                 @Override
  12.                 public void onPathStarted(final PathModifier pPathModifier, final IEntity pEntity) {
  13.                         //Debug.d("onPathStarted");
  14.                 }
  15.  
  16.                 @Override
  17.                 public void onPathWaypointStarted(final PathModifier pPathModifier, final IEntity pEntity, final int pWaypointIndex) {
  18.                         switch(pWaypointIndex) {
  19.                         case 0:
  20.                                 papillon1.animate(new long[]{200, 200, 200}, 27, 29, true);
  21.                                 break;
  22.                         case 1:
  23.                                 papillon1.animate(new long[]{200, 200, 200}, 15, 17, true);
  24.                                 break;
  25.                         case 2:
  26.                                 papillon1.animate(new long[]{200, 200, 200}, 1, 3, true);
  27.                                 break;
  28.                         case 3:
  29.                                 papillon1.animate(new long[]{200, 200, 200}, 5, 7, true);
  30.                                 break;
  31.                 }
  32.                         //Debug.d("onPathWaypointStarted:  " + pWaypointIndex);
  33.                 }
  34.  
  35.                 @Override
  36.                 public void onPathWaypointFinished(final PathModifier pPathModifier, final IEntity pEntity, final int pWaypointIndex) {
  37.                         //Debug.d("onPathWaypointFinished: " + pWaypointIndex);
  38.                         //player.stopAnimation();              
  39.                         papillon1.setCurrentTileIndex(4);
  40.                         PathExtendedPathModifier.pauseModifier();
  41.                         //onResume();
  42.                        
  43.                 }
  44.  
  45.                 @Override
  46.                 public void onPathFinished(final PathModifier pPathModifier, final IEntity pEntity) {
  47.                         //Debug.d("onPathFinished");
  48.                         papillon1.stopAnimation();                                             
  49.                 }
  50.                
  51.         },new IExtendedPathModifierListener(){
  52.  
  53.                 @Override
  54.                 public void onPause(PathModifier pPathModifier) {
  55.                         // TODO Auto-generated method stub
  56.  
  57.                 }
  58.  
  59.                 @Override
  60.                 public void onResume(PathModifier pPathModifier) {
  61.                         // TODO Auto-generated method stub
  62.                        
  63.                 }}, EaseLinear.getInstance());
  64.  
  65.        
  66.         papillon1.registerEntityModifier(
  67.                        
  68.                 new LoopEntityModifier(PathExtendedPathModifier
  69.                                
  70. )
  71.         );     
  72.        
  73.         mScene.attachChild(papillon1);
Parsed in 0.044 seconds, using GeSHi 1.0.8.4


It's really possible to pause PathModifier without pause all game ????????????
raphael
 
Posts: 14
Joined: Wed Feb 20, 2013 6:08 pm

Re: Timeshifting and Pausing PathModifier

Postby shihab_returns » Sat Apr 20, 2013 8:58 pm

not clear ...your question
shihab_returns
 
Posts: 101
Joined: Tue Oct 16, 2012 8:48 am
Location: Dhaka


Return to Features

Who is online

Users browsing this forum: Alexa [Bot], mavericks and 6 guests