Question

I am Working on Android cocos 2d and i have 8-10 CCSprites which has to scroll horizontally by click of each sprite their is next CCLayer to load so need to Add ScrollView in CCLayer but i am not getting how to do this i am using cocos2d-android.jar

i am using this code but not working :-

final Activity mActivity=CCDirector.sharedDirector().getActivity();

        final View view= LayoutInflater.from(mActivity).inflate(R.layout.level_scroll,null);

        mActivity.runOnUiThread(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            mActivity.addContentView(view, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

        }
        });
Was it helpful?

Solution

This is the layer to which you have to add all sprites:

public class ItemScrollLayer extends CCLayer{

        ArrayList<ExtraProjectileData> spirtes;
        float scaleX,scaleY;
        public ItemScrollLayer(float scaleX,float scaleY,ArrayList<ExtraProjectileData> sprites,float screensize)
        {
            this.scaleX = scaleX;
            this.scaleY = scaleY;
            this.spirtes = sprites;

            float horizontal_distance = 140*scaleX;


            for(int i = 0;i<sprites.size();i++)
            {
                CCSprite indi_sprite = sprites.get(i).getProjectile();
                indi_sprite.setScale(GameActivity.aspect_Scale(indi_sprite, scaleX, scaleY));

                indi_sprite.setPosition(horizontal_distance+(i*screensize),150*scaleY);
                addChild(indi_sprite);
            }
        }
    }

the code to move layer placed in on Touch of my MainLayer:

@Override
    public boolean ccTouchesMoved(MotionEvent event)
    {

        System.out.println("Touches Moved Called for UpgradeMenu");
        CGPoint LocationMoved = CCDirector.sharedDirector().convertToGL(CGPoint.make(event.getX(), event.getY()));
        float difference = LocationMoved.x - PrevTouchLocation.x;

        float posX = scroll_layer.getPosition().x + difference;
        scroll_layer.setPosition(CGPoint.make(posX, 0));
        if(posX > 0){
            scroll_layer.setPosition(CGPoint.zero());
        }
        else if(posX < (-size.width)*(sprites.size()-1)){
            System.out.println("Right Limit Exceeded");
            scroll_layer.setPosition((-size.width)*(sprites.size()-1),0);
        }
        if(difference < -5*GameActivity.VEL_FACTOR){
            direction = -1;
        }
        else if(difference > 5*GameActivity.VEL_FACTOR){
            direction = 1;
        }
        PrevTouchLocation = LocationMoved;

        return true;
    }
    @Override
    public boolean ccTouchesEnded(MotionEvent event)
    {
        endLocation = CCDirector.sharedDirector().convertToGL(CGPoint.make(event.getX(), event.getY()));
        if(!moving)
        {
            float total = startLocation.x-endLocation.x;
            if(direction == 1 && !(counter <=0))
            {

                CGPoint move_pos = CGPoint.make(size.width+total, 0);
                CCMoveBy go_left = CCMoveBy.action(0.5f, move_pos);
                CCCallFuncN regulator = CCCallFuncN.action(this, "regulator");
                CCSequence seq = CCSequence.actions(go_left, regulator);
                moving = true;
                scroll_layer.runAction(seq);


                counter--;
                projectilePriceLabel.setString(getCurrentPrice());


            }
            else if(direction == -1 && !(counter >= sprites.size()-1))
            {

                CGPoint move_pos = CGPoint.make(-size.width+total, 0);
                CCMoveBy go_right = CCMoveBy.action(0.5f, move_pos);
                CCCallFuncN regulator = CCCallFuncN.action(this, "regulator");
                CCSequence seq = CCSequence.actions(go_right, regulator);
                moving = true;
                scroll_layer.runAction(seq);

                counter++;
                projectilePriceLabel.setString(getCurrentPrice());



            }
        }
        PrevTouchLocation = CGPoint.zero();
        return true;
    }

you can edit as per your requirement

OTHER TIPS

Check this, its for vertical scrolling. You just need to change little to achieve.

https://stackoverflow.com/a/12056450/1614340

Let me know if you can't get any idea from this I will provide code.

You can do by adding all sprites to a parent layer then move this parent layer by using MoveBy modifier in ccTouchesMoved

sorry for delay. You have to initialize PrevTouchLocation before you use that as mention below.

CGPoint PrevTouchLocation = CGPoint.zero();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top