• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Issue with movement in my game.

glascor

Lurker
HI,

I have a shooter game. I can move from left to right but in order to move right I have to press in the far right corner to move to the right. Anywhere else it moves to the left. I am hoping to have it setup at center and if you press to the left or right it will move accordingly.

Any help would be greatly appreciated. The code is listed below.

Thanks,



/*
* model of slider controller
*/
public class SliderPaddle extends CCLayer {
/** sprites */
private CCSprite m_sprtBar;
private CCSprite m_sprtKnob;
private float m_ftValue = 0.5f;
private Limit m_limitValue = Limit.make(0f, 1f);
private float m_ftBarLength = 1f;
private boolean m_bKnobCaptured = false;
/** callback */
private Object m_target;
private String m_TouchMoveMethod;
private String m_TouchEndedMethod;
/** callback functions must be one parameter that specifies float */
public void setCallback(Object target, String strMove, String strEnded ){
m_target = target;
m_TouchMoveMethod = strMove;
m_TouchEndedMethod = strEnded;
}
public static SliderPaddle make( String strBar, String strKnob, Limit limit ){
return new SliderPaddle( strBar, strKnob, limit );
}
public SliderPaddle( String strBar, String strKnob, Limit limit ){
// create sprite
m_sprtBar = CCSprite.sprite( strBar );
Macros.LOCATE_NODE(this, m_sprtBar, 0, 0);
m_sprtKnob = CCSprite.sprite( strKnob );
Macros.CORRECT_SCALE(m_sprtKnob);
addChild(m_sprtKnob);
// initialize
this.setIsTouchEnabled(true);
m_limitValue = limit;
m_ftBarLength = m_sprtBar.getContentSize().width;
update();
}
/** set value */
public void setValue( float v ){
m_ftValue = v;
if( m_limitValue.isLeft(m_ftValue) ) m_ftValue = m_limitValue.lower;
if( m_limitValue.isRight(m_ftValue) ) m_ftValue = m_limitValue.upper;
update();
}
public float getValue(){
return m_ftValue;
}
/** update knob`s position */
private void update(){
Macros.POSITION_NODE(m_sprtKnob, -m_ftBarLength/2 + m_ftBarLength*(m_ftValue-m_limitValue.lower/m_limitValue.length()), 0);
}
public boolean ccTouchesBegan(MotionEvent event){
CGPoint ptTouch = this.convertToNodeSpace (
CCDirector.sharedDirector().convertToGL(
CGPoint.make(event.getX(), event.getY())));
if( !CGRect.containsPoint( m_sprtKnob.getBoundingBox(), ptTouch) ){
return CCTouchDispatcher.kEventIgnored;
}
m_bKnobCaptured = true;
return CCTouchDispatcher.kEventHandled;
}
public boolean ccTouchesMoved(MotionEvent event){
CGPoint ptTouch = Macros.REAL_TO_LOGICAL(convertToNodeSpace( CCDirector.sharedDirector().convertToGL(CGPoint.make(event.getX(), event.getY()))));
if( !m_bKnobCaptured ){
return CCTouchDispatcher.kEventIgnored;
}
float ftOffset = ptTouch.x;
if( ftOffset < -m_ftBarLength/2 ) ftOffset = -m_ftBarLength/2;
if( ftOffset > m_ftBarLength/2 ) ftOffset = m_ftBarLength/2;
Macros.POSITION_NODE(m_sprtKnob, ftOffset, 0);
m_ftValue = m_limitValue.lower + m_limitValue.length() * (ftOffset+m_ftBarLength/2) / m_ftBarLength;
// call event handler
try {
Class<?> cls = m_target.getClass();
Method invocation = cls.getMethod(m_TouchMoveMethod, float.class);
try {
invocation.invoke(m_target, m_ftValue);
} catch (Exception e) {
e.printStackTrace();
}
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
return CCTouchDispatcher.kEventHandled;
}
public boolean ccTouchesEnded(MotionEvent event){
if( !m_bKnobCaptured ) return CCTouchDispatcher.kEventIgnored;
m_bKnobCaptured = false;
try {
Class<?> cls = m_target.getClass();
Method invocation = cls.getMethod(m_TouchEndedMethod, float.class);
try {
invocation.invoke(m_target, m_ftValue);
} catch (Exception e) {
e.printStackTrace();
}
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
return CCTouchDispatcher.kEventHandled;
}
@Override
protected void registerWithTouchDispatcher() {
CCTouchDispatcher.sharedDispatcher().addTargetedDelegate(this, ccMacros.INT_MIN+1, true);
}
}
 
hey @glascor i made similar movement in my game but in Unity3d c#script it took a lots of time if you want i can tell how to pm me.
 
Back
Top Bottom