tutorial español curso cocos2d android cocos2d-android

español - Adición de fondo de paralaje sin fin en cocos2d android



cocos2d tutorial español (6)

Este es un truco para que esto suceda. Puede usar el png grande y trabajar en él o consultar el código de prueba de muestra que está disponible en la biblioteca coocs2d-android

CCSprite background = CCSprite.sprite("background.png"); // create a void node, a parent node CCParallaxNode voidNode = CCParallaxNode.node(); // background image is moved at a ratio of 0.4x, 0.5y voidNode.addChild(background, -1, 0.4f, 0.5f, 0, 0); // write your own code for the parallax node CCIntervalAction goUp = CCMoveBy.action(4, CGPoint.make(0,-200)); CCIntervalAction goDown = goUp.reverse(); CCIntervalAction go = CCMoveBy.action(8, CGPoint.make(-1000, 0)); CCIntervalAction goBack = go.reverse(); CCIntervalAction seq = CCSequence.actions(goUp, go, goDown, goBack); voidNode.runAction(CCRepeatForever.action(seq)); addChild(voidNode);

Estoy trabajando en CoCos2d con Android. Quiero agregar un fondo de desplazamiento sin fin a mi pantalla usando CCParallaxNode. Puedo agregar fondo y moverlo, pero después de completar esa acción de movimiento, la pantalla se vuelve negra. ¿Alguien me puede ayudar?

Mi codigo es

CCParallaxNode parallaxNode; CCSprite spacedust1; CCSprite spacedust2; CCSprite planetsunrise; CCSprite galaxy; CCSprite spacialanomaly; CCSprite spacialanomaly2; parallaxNode = CCParallaxNode.node(); spacedust1 = CCSprite.sprite("bg_front_spacedust.png"); spacedust2 = CCSprite.sprite("bg_front_spacedust.png"); planetsunrise = CCSprite.sprite("bg_planetsunrise.png"); galaxy = CCSprite.sprite("bg_galaxy.png"); spacialanomaly = CCSprite.sprite("bg_spacialanomaly.png"); spacialanomaly2 = CCSprite.sprite("bg_spacialanomaly2.png"); // 3) Determine relative movement speeds for space dust and background // CGPoint cgPoint = CGPoint.ccp(0.1, 0.1); CGPoint dustSpeed = CGPoint.ccp(10, 10); CGPoint bgSpeed = CGPoint.ccp(5, 5); // CGPoint bgSpeed = ccp(0.05, 0.05); parallaxNode.addChild(spacedust1, 0, dustSpeed.x, dustSpeed.y, 0, winSize.height / 2); parallaxNode.addChild(spacedust2, 0, dustSpeed.x, dustSpeed.y, spacedust1.getContentSize().width, winSize.height / 2); parallaxNode.addChild(galaxy, -1, bgSpeed.x, bgSpeed.y, 0, 10); parallaxNode.addChild(planetsunrise, -1, bgSpeed.x, bgSpeed.y, 600, 5); parallaxNode .addChild(spacialanomaly, -1, bgSpeed.x, bgSpeed.y, 900, 20); parallaxNode.addChild(spacialanomaly2, -1, bgSpeed.x, bgSpeed.y, 1500, 30); CCIntervalAction go = CCMoveBy.action(4, CGPoint.ccp(winSize.width, 0)); CCIntervalAction goBack = go.reverse(); CCIntervalAction seq = CCSequence.actions(go, goBack); CCRepeatForever action = CCRepeatForever.action(goBack); parallaxNode.runAction(action);


Llame a este método de la clase Constructor. Encontré este truco del Ejemplo: "shotingblock-master" disponible en github ...

private void endlessBackground() { // Create the two background sprites which will alternate _oddBackground = CCSprite.sprite("blue_background.png"); _evenBackground = CCSprite.sprite("blue_background.png"); // One starts dead centre and one starts exactly one screen height above oddBackground.setPosition(_winSize.width / 2, _winSize.height / 2); evenBackground.setPosition(_winSize.width / 2, _winSize.height + (_winSize.height / 2)); // Schedule the scrolling action schedule("scroll"); // Add sprites to the layer addChild(_oddBackground).addChild(_evenBackground); } public void scroll(float dt) { // move them 100*dt pixels down _oddBackground.setPosition(_oddBackground.getPosition().x, _oddBackground.getPosition().y - 150 * dt); _evenBackground.setPosition(_evenBackground.getPosition().x, _evenBackground.getPosition().y - 150 * dt); // reset position when they are off from view. if (_oddBackground.getPosition().y < -_winSize.height / 2) { _oddBackground.setPosition(_winSize.width / 2, _winSize.height / 2); _evenBackground.setPosition(_winSize.width / 2, _winSize.height + (_winSize.height / 2)); } }

}

Funciona excelente en mi caso. Puede ser que te ayude por completo.


Lo veo ya que ni una sola respuesta te funcionó. Proporcionaré un código simple que le ayudará para el fondo de desplazamiento de Parralax.

Añade este código en tu juego de constructor de capas.

background1 = CCSprite.sprite("bg2.png"); background2 = CCSprite.sprite("bg2.png"); background1.setPosition(CGPoint.ccp(winSize.width*0.5f,winSize.height*0.5f)); addChild(background1); background2.setPosition(CGPoint.ccp(winSize.width+winSize.width*0.5f,winSize.height*0.5f)); addChild(background2);

y un método de desplazamiento que se programa cada milisegundo. añade esto en el constructor

this.schedule("scroll");

Y ahora el método de desplazamiento.

public void scroll(float dt) { CGPoint pos1 = background1.getPosition(); CGPoint pos2 = background2.getPosition(); pos1.x -= 5.0f; pos2.x -= 5.0f; if(pos1.x <=-(winSize.width*0.5f) ) { pos1.x = pos2.x + winSize.width; } if(pos2.x <=-(winSize.width*0.5f) ) { pos2.x = pos1.x + winSize.width; } background1.setPosition(pos1); background2.setPosition(pos2); }

Marque mi respuesta si funcionó.


Parece que la acción CCRepeatForever solo la ejecuta goBack , lo que significa que no se está invirtiendo. Intenta lo siguiente:

CCIntervalAction go = CCMoveBy.action(4, CGPoint.ccp(winSize.width, 0)); CCIntervalAction goBack = go.reverse(); CCIntervalAction seq = CCSequence.actions(go, goBack); CCRepeatForever action = CCRepeatForever.action(seq); // change to seq instead of goBack parallaxNode.runAction(action);


Por favor revise el siguiente enlace para ver el fondo infinito vertical de Parallax: http://kalpeshsantoki.blogspot.in/2014/07/create-vertical-endless-parallax.html

CGSize winSize = CCDirector.sharedDirector().displaySize(); //I made graphics for screen 720*1200....so I made this dynamic scale to support multiple screens float sX = winSize.width / 720.0f; float sY = winSize.height / 1200.0f; background = CCVerticalParallaxNode.node(sX, sY, true); background.addEntity(1f, "background.png", 0); background.addEntity(3, "road_simple.png", winSize.width / 2); background.addEntity(1.7f, "road_side.png", 0); addChild(background);


intenta usar esto:

CCTexture2D *texture = CCTextureCache::sharedTextureCache()->addImage("pic.png"); ccTexParams params = {GL_LINEAR, GL_LINEAR, GL_REPEAT, GL_REPEAT}; texture->setTexParameters(&params); CCSprite *sprite = CCSprite::spriteWithTexture(texture, CCRectMake(0, 0, 90, 90));

y asegúrese de que la altura y el ancho de la imagen deben tener una potencia de 2.