Getting Behaviors in TypeScript

1 favourites
  • 2 posts
  • How do we get behaviors on an ISpriteInstance subclass in TypeScript?

    When attempting to get the behaviors property of my subclass, I see the following error:

    Property 'behaviors' does not exist on type 'DragItem'.

    Here's my truncated code:

    main.ts

    import DragItem from './DragItem.js';
    
    runOnStartup(async runtime => {
    	runtime.objects.DragItem.setInstanceClass(DragItem);
    	// ...
    });
    

    DragItem.ts

    export default class DragItem extends ISpriteInstance {
    	constructor() {
    		super();
    		const dragDrop = this.behaviors.DragDrop; // Error
    	}
    }
    

    instanceTypes.d.ts

    (Generated)

    class __DragItemBehaviors<InstType> {
    	DragDrop: IDragDropBehaviorInstance<InstType>;
    }
    
  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Here's the solution for anyone else that overlooked Ashley's article on using TypeScript in Construct:

    When using TypeScript, Construct generates a special class representing an instance for every object type and family in the project. This includes type definitions for things like the instance variables, behaviors and effects specific to that object. These classes are all in the InstanceType namespace with the name of the object.

    So, the solution was to extend DragItem from InstanceType.DragItem.

    export default class DragItem extends InstanceType.DragItem {
    	// ...
    }
    
Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)