@@ -41,8 +41,10 @@ export default class Character extends Component {
4141 const { keys, store } = this . props ;
4242 const { body } = this . body ;
4343
44- const shouldMoveStageLeft = body . position . x < 448 && store . stageX < 0 ;
45- const shouldMoveStageRight = body . position . x > 448 && store . stageX > - 1024 ;
44+ const midPoint = Math . abs ( store . stageX ) + 448 ;
45+
46+ const shouldMoveStageLeft = body . position . x < midPoint && store . stageX < 0 ;
47+ const shouldMoveStageRight = body . position . x > midPoint && store . stageX > - 1024 ;
4648
4749 if ( body . velocity . y === 0 ) {
4850 this . isJumping = false ;
@@ -62,18 +64,18 @@ export default class Character extends Component {
6264 if ( keys . isDown ( keys . LEFT ) || gamepad . button ( 0 , 'button 14' ) ) {
6365 if ( shouldMoveStageLeft ) {
6466 store . setStageX ( store . stageX + 5 ) ;
65- } else {
66- this . move ( body , - 5 ) ;
6767 }
6868
69+ this . move ( body , - 5 ) ;
70+
6971 characterState = 1 ;
7072 } else if ( keys . isDown ( keys . RIGHT ) || gamepad . button ( 0 , 'button 15' ) ) {
7173 if ( shouldMoveStageRight ) {
7274 store . setStageX ( store . stageX - 5 ) ;
73- } else {
74- this . move ( body , 5 ) ;
7575 }
7676
77+ this . move ( body , 5 ) ;
78+
7779 characterState = 0 ;
7880 }
7981
@@ -82,14 +84,22 @@ export default class Character extends Component {
8284 this . setState ( {
8385 characterState,
8486 } ) ;
87+ } else {
88+ const targetX = store . stageX + ( this . lastX - body . position . x ) ;
89+ if ( shouldMoveStageLeft || shouldMoveStageRight ) {
90+ store . setStageX ( targetX ) ;
91+ }
8592 }
93+
94+ this . lastX = body . position . x ;
8695 } ;
8796
8897 constructor ( props ) {
8998 super ( props ) ;
9099
91100 this . loopID = null ;
92101 this . isJumping = false ;
102+ this . lastX = 0 ;
93103
94104 this . state = {
95105 characterState : 2 ,
@@ -105,13 +115,14 @@ export default class Character extends Component {
105115 }
106116
107117 getWrapperStyles ( ) {
108- const { characterPosition } = this . props . store ;
118+ const { characterPosition, stageX } = this . props . store ;
109119 const { scale } = this . context ;
110120 const { x, y } = characterPosition ;
121+ const targetX = x + stageX ;
111122
112123 return {
113124 position : 'absolute' ,
114- transform : `translate(${ x * scale } px, ${ y * scale } px)` ,
125+ transform : `translate(${ targetX * scale } px, ${ y * scale } px)` ,
115126 transformOrigin : 'left top' ,
116127 } ;
117128 }
0 commit comments