www.webdeveloper.com
Results 1 to 2 of 2

Thread: Create own events

  1. #1
    Join Date
    Nov 2012
    Posts
    1

    Smile Create own events

    Hi, i got a class that updates and renders a button, wich i created.

    My Button class looks like this:

    Code:
    var Button = function(x,y,width,height,text){
    	
    	this.rect = new Rectangle(x,y,width,height);
    	
    	this.text = text;
    	
    	this.hover = true;
    	this.pressed = false;
    	
    	this.update = function(input){
    		this.hover = this.rect.Contains(input.x, input.y);
    		
    		if(this.hover && input.m_left && !this.pressed){
    			this.pressed = true;
    			this.clicked();
    		}
    		
    		if(!input.m_left){
    			this.pressed = false;
    		}
    	
    		
    	};
    	
    	this.render = function(ctx){
    		this.hover? this.rect.Draw(ctx, "blue"):this.rect.Draw(ctx, "red");
    	};
    	
    	this.clicked = function(){};
    	
    };
    and the basic class:

    Code:
    var Basic = function(){
    	this.x = 100;
    	
    	this.b1 = new Button(0,0,200,50,"lol");
    	this.b1.clicked = function(){
    		this.rect.x += Basic.x;
    		alert(this.x);
    	};
    	
    	this.update = function(input){
    		this.b1.update(input);
    	};
    	
    	this.render = function(ctx){
    		this.b1.render(ctx);
    	};
    };
    The problem is, that Basic.x (i also tried this.x) is undefined, because the clicked funtion is executed in the button class.
    How can i access elements from the Basic class, without giving them as argument to the Button class?

    Thx

  2. #2
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    use the ole this n that, and "new "

    Code:
    var Basic = new function(){
    	this.x = 100;
    	var that=this;
    	this.b1 = new Button(0,0,200,50,"lol");
    	this.b1.clicked = function(){
    		this.b1.rect.x += that.x;
    		alert(that.x);
    ...
    	};

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles