Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

317
Views
Cocos Creator - Collision not working for nodes added from JS

So I'm trying to dynamically add some obstacles to the game, now this is the whole code related to that:

cc.Class({
    extends: cc.Component,

    properties: {
        ObstaclesGap: 180,
        Alive: true,
        XChange: 300,
        LastLowerX: 0,
        UpperObstacleSpritePrefab: cc.Prefab,
        LowerObstacleSpritePrefab: cc.Prefab,
    },

    generateObstacle() {
        // Creation of obstacles
        const lowerObstacle = cc.instantiate(this.LowerObstacleSpritePrefab);
        const upperObstacle = cc.instantiate(this.UpperObstacleSpritePrefab);

        // Addition to canvas
        this.node.addChild(lowerObstacle);
        this.node.addChild(upperObstacle);

        // Setting sizes
        lowerObstacle.width = 60;
        upperObstacle.width = 60;

        /*
            Generates a number between 1 - 10, and multiply the lower obstacle's height with that number.
            The rest is then equals to the upper obstacle's height.
            All of that with a remaining 180 gap between them all of the time (which is 3 times the size of the bird)
        */
        const rndNumber1t10 = Math.floor(Math.random() * 10)+1;
        lowerObstacleHeight = 540 * (rndNumber1t10 / 10);
        upperObstacleHeight = 540 * ((10 - rndNumber1t10) / 10);
        lowerObstacle.height = lowerObstacleHeight;
        upperObstacle.height = upperObstacleHeight;

        // Preperations for next obstacle positions

        lowerObstacle.getComponent(cc.PhysicsBoxCollider).size = cc.Size(60, lowerObstacleHeight)

        upperObstacle.getComponent(cc.PhysicsBoxCollider).size = cc.Size(60, upperObstacleHeight)

        this.LastLowerX += this.XChange;

        // Setting positions with a gap between the obstacles (720 - 180 = 540)
        lowerObstacle.setPosition(cc.v2(parseInt((-this.node.x) + this.LastLowerX), parseInt((-this.node.y) + 100 + lowerObstacleHeight / 2)));
        upperObstacle.setPosition(cc.v2(parseInt((-this.node.x) + this.LastLowerX), parseInt((-this.node.y) + 720)));

    },

    onLoad() {
        for (let i = 0; i < 1; i++) {
            this.generateObstacle();
        }
    },

    start() {

    },

    // update (dt) {},
});

This is my Group Manager:

enter image description here

And here's an example for the one of the Prefebs:

enter image description here

The problem is that the collision does not happen between the bird and the obstacles, but if I manually drag on to the game, it will collide so there must be some problem with my code.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!