cocos2dx js

  • rashasalah
    Likes 0

    Problem Description

    this.addChild (spirte,0) is not a function

    error always appear if i want to create a spirte in a function or event

    please i want a solution

    another question : can u supply me with a link or tutorial for coding cocos-2dx js the plateform like in the 84 video series in youtube

  • Sonar Systems admin
    Likes 0

    Please post your code and we will take a look.

  • rashasalah
    Likes 0

    
    //////// app.js
    var HelloWorldLayer = cc.Layer.extend({
        sprite:null,
        ctor:function () {
            //////////////////////////////
            // 1. super init first
            this._super();
    
            
            var size = cc.winSize;
            var x=0;
            var a =50 ;
            var b = 0;
            var zz=0;
            var s1=-21.5;
            var s11 = -2.5;
            var myArray = [];
            var f1 =0; var f2=0;var f3=0; var f4=0;
            var mySprite;
            var menu ;
            for(var i=-0.5;i<2.5;i++){
           for(var j=-9;j<=-7;j++){
             mySprite = new cc.Sprite.create(res.HelloWorld_png);
             mySprite.setAnchorPoint(cc.p(j-0.5, i));
             mySprite.setPosition(cc.p(size.width / 2, size.height / 2));
             myArray.push(mySprite);
              this.addChild(mySprite, 0);
    }}
    	// var menu = new cc.Menu(myArray);
    // this.addChild(myArray);
    		var sprite_action;
             var sprite = new cc.Sprite.create(res.Clos_png);
             sprite.setAnchorPoint(cc.p(s1,s11));
             sprite.setPosition(cc.p(size.width / 2, size.height / 2));
             this.addChild(sprite, 0);
      
            var menuItem1 = new cc.MenuItemImage(res.Hello_png, res.CloseSelected_png, imageFunc1);
            var menuItem2 = new cc.MenuItemImage(res.Back_png, res.CloseSelected_png, imageFunc2);
             var menuItem3 = new cc.MenuItemImage(res.Down_png, res.CloseSelected_png, imageFunc3);
             var menuItem4 = new cc.MenuItemImage(res.Up_png, res.CloseSelected_png, imageFunc4);
             var menuItem5 = new cc.MenuItemImage(res.Lamp_png, res.CloseSelected_png, imageFunc5);
            var menu = new cc.Menu(menuItem1,menuItem2,menuItem3,menuItem4,menuItem5);
    //,menuItem6,menuItem7,menuItem8,menuItem9,menuItem10,menuItem11
            // menu.alignItemsVerticallyWithPadding(50);
            // menuItem1.onMouseDown(imageFunc);
              menu.alignItemsInRows(2,2,1);
            menu.setPosition(cc.p(600, 700)); 
            this.addChild(menu);
       //   	var spritex ;
       //   	var spritex = new cc.Sprite.create(res.CloseNormal_png);
    		 // spritex.setAnchorPoint(cc.p(-9.5,1.5));
       //      spritex.setPosition(cc.p(size.width / 2, size.height / 2));	     
       //       this.addChild(spritex, 0);
               var imageFunc5 = function()
    {			
    		var spritex = new cc.Sprite.create(res.CloseNormal_png);
    		 spritex.setAnchorPoint(cc.p(-9.5,1.5));
            spritex.setPosition(cc.p(size.width / 2, size.height / 2));
            this.addChild(spritex,1);     
                     				
    }
    // x++  
    //      if(x==1)
    		// // if(x==1)
    		// // 	 {
    		// 	 var 	 sprite2 = new cc.Sprite.create(res.CloseNormal_png);
    		// sprite2.setAnchorPoint(cc.p(-9.5,1.5));
      //        sprite2.setPosition(cc.p(size.width / 2, size.height / 2));
      //     var 	 sprite = new cc.Sprite.create(res.CloseNormal_png);
    		// sprite.setAnchorPoint(cc.p(-9.5,1.5));
      //        sprite.setPosition(cc.p(size.width / 2, size.height / 2));
      //         if(x==1)
      //        this.addChild(sprite, 0); 
      // //    } 
     
    
       if (cc.sys.capabilities.hasOwnProperty('mouse'))
            {
            	cc.eventManager.addListener(
            	{
            		event: cc.EventListener.MOUSE,
    
            		onMouseDown: function(event)
            		{
            			if (event.getButton() == cc.EventMouse.BUTTON_LEFT)
            			{	
            				if(x==1&&f1==1)
            				{
            					imageFunc5();
       //      					   var spritex = new cc.Sprite.create(res.CloseNormal_png);
    		 // spritex.setAnchorPoint(cc.p(-9.5,1.5));
       //      spritex.setPosition(cc.p(size.width / 2, size.height / 2));
       //      this.addChild(spritex,1);   
            				}
            				else {
            				 if(menuItem1.isSelected())
            				        			{imageFunc1();}	 
            					else if(menuItem2.isSelected())
            				        				 {imageFunc2();}
            				    else if(menuItem3.isSelected())
            				        				{imageFunc3();} 
            			    	else if(menuItem4.isSelected())
            				        				{imageFunc4();} 
            				    else if(menuItem5.isSelected())
            				        				{imageFunc5();} 
        
            			}}
            		}
            	}, this);
            }
    
               var imageFunc1 = function()
    {
    	if (s1>-27.5)
    	{
    		if(s1==-24.5)
    			{f1=1;}
    	 sprite_action = cc.MoveBy.create(0.5, cc.p(a, 0));
          	sprite.runAction(sprite_action); s1-=3;
          }
          
    }
                 var imageFunc2 = function()
    {
    	if(s1<-21.5)
    	 {sprite_action = cc.MoveBy.create(0.5, cc.p(-a, 0));
          	sprite.runAction(sprite_action); s1+=3;
          }
          
    }
               var imageFunc3 = function()
    {				if(s11<3.5)
    	{		if(s11==0.5)
    				{x=1;}
             		 var sprite_action = cc.MoveBy.create(0.5, cc.p(0, -a));
        		     sprite.runAction(sprite_action); s11+=3; 
    
        		 }
             				
    }
               var imageFunc4 = function()
    {         	
    			if(s11>-2.5)
    	{
    	 var sprite_action = cc.MoveBy.create(0.5, cc.p(0, a));
        		     sprite.runAction(sprite_action); s11-=3;
        		 }
             				
    }
    // var imageFunc5= function(){
    // 	var 	 sprite = new cc.Sprite.create(res.CloseNormal_png);
    // 		sprite.setAnchorPoint(cc.p(-9.5,1.5));
    //          sprite.setPosition(cc.p(size.width / 2, size.height / 2));
    //          this.addChild(sprite, 0); 
    // }
    
    	// if(f2==1)
    	// 		 {
     //      var 	 sprite = new cc.Sprite.create(res.CloseNormal_png);
    	// 	sprite.setAnchorPoint(cc.p(-9.5,1.5));
     //         sprite.setPosition(cc.p(size.width / 2, size.height / 2));
     //         this.addChild(sprite, 0); 
     //     } 
            return true;
        }
    
    });
    
    var HelloWorldScene = cc.Scene.extend({
        onEnter:function () {
            this._super();
            var layer = new HelloWorldLayer();
            this.addChild(layer);
        }
    });
    
    ////////resource.js
    var folder = "";
    
    if (!cc.sys.isNative)
    {
        folder = "res/mediumRes/";
    }
    
    var res = {
        HelloWorld_png : folder + "HelloWorld.png",
        CloseNormal_png : folder + "CloseNormal.png",
        CloseSelected_png : folder+ "Ground.png",
        Hell_png : folder + "Hell.png",
        Clos_png : folder + "index.png",
        End_png : folder + "End.png",
        Back_png : folder + "Back.png",
        Down_png : folder + "Down.png",
        Up_png : folder + "Up.png",
        Hello_png : folder + "Forward.png",
        Lamp_png : folder + "Lamp.png",
    };
    
    var g_resources = [];
    for (var i in res) {
        g_resources.push(res[i]);
    }
    
    

    this is my code  for app.js & resourse.js

  • Sonar Systems admin
    Likes 0

    Put the functions inside fo the helloworldlayer

  • rashasalah
    Likes 0

    itis already inside helloworldlayer ?!


  • Sonar Systems admin
    Likes 0

    Oh yh it is, try creating a global variable inside HelloWorldLayer and assign this to it within the  and use that to add children to the scene  

  • rashasalah
    Likes 0

    I have already tried this … i don’t want to try , i asked sonarlearing from facebook , mail , youtube &here beacuse i really need a solution not to try ways i have already checked it ! i thought that if i put the code above  u will running it and give me solution and reason for my error .. so please can u run my code on the tutorial6 multiresolution and give me solution please <« https://www.4shared.com/rar/ytFFCKqhce/Cocos2d-JS-v3-Tutorial-6---Mul.html>> .. i hope that i waited for u few  days ago this is my source code … i need to fix this error 

  • Sonar Systems admin
    Likes 0

    Unfortunately we cannot run everyones code due to time restrictions.

     

    Try doing HelloWorldLayer.addchild

  • rashasalah
    Likes 0

    how to make a score label and update itis value when move from place to another ??

  • Sonar Systems admin
    Likes 0

    Move from place to another?

Login to reply